#Understanding comptime
1 messages · Page 1 of 1 (latest)
comptime if (boolean) {...};
that wouldn't work tho
and what corner suggests if you need to make the code itself runtime
yep
wouldn't comptime if be all comptime?
it would
yeah, which is what the original question is asking about
I thought it was asking about conditional runtime code
just look at the g'damn question
ye, I see it...
Yeah I agree with Chuck's reading
Slightly more in-depth
if (cond) will only include one branch if cond is comptime-known. There are a few ways you can do that:
- you can explicitly mark it using the
comptimekeyword, which is what corner suggested (although he used unnecessary parens,comptimeisn't a function: justif (comptime expr)) - it'll be automatically evaluated at compile-time if it's a simple expression whose value is known at compile-time. For instance, if you check
builtin.os.tag == .linux, that'll be automatically checked at comptime. By "simple expression" here I basically mean "not a function call" (although calls toinlinefunctions do get comptime-evaluated; or rather, the function's body is effectively pasted straight into the condition and evaluated there)
just right click the question on the left and mark as "closed"
if you don't, eventually somebody on the mod team will
comptime has really terrible precedence, I've seen situations where it only applied it to the lhs of a comparison
that's why I always stick parens on it
in if(comptime value == f()) I've seen it only evaluate the lhs at compile time
should be defined as having the lowest precedence
definitely, and try needs a higher one
lol
so that I can do try f() orelse a
anyway, end of discussion, question is answered