#Any significant difference between `switch` and `comptime switch`?

1 messages · Page 1 of 1 (latest)

vivid helm
#

More specifically, is there any difference between the two statements when inside an inline for?

fast crypt
#

inline for does not imply the body runs at comptime, it just means its unrolled

lime wadi
#

comptime switch forces the switch expression to be evaluated at comptime. inline for creates multiple copies of the loop body at comptime, but the loop body may still compute and switch on runtime values.

fast crypt
#

comptime switch makes the whole switch comptime not just the condition

lime wadi
#

right facepalm

vivid helm
#

so comptime switch works the same as another other comptime expr

lime wadi
#

yes

vivid helm
#

i was wondering mostly because im trying to build a semi-generic build script and was using something to the effect of

inline for (sources) |src| {
  const step = switch (src.type) {
    // ...
  }
}
#

(ofc b.add...() returns a pointer so comptime switch is invalid here but i was curious nonetheless)

covert glacier
#

keep on mind that build code is effectively comptime already

#

and that comptime uses an interpreter in the compiler

#

it's probably faster to not use comptime when possible in build.zig

#

it will run about once per build anyways

vivid helm
#

yeah that's fair enough