#Understanding comptime

1 messages · Page 1 of 1 (latest)

tacit hull
#

if(comptime(boolean))

cursive tusk
#

comptime if (boolean) {...};

tacit hull
#

or that I guess

#

up to you

stoic magnet
#

that wouldn't work tho

cursive tusk
#

and what corner suggests if you need to make the code itself runtime

tacit hull
#

yep

stoic magnet
#

wouldn't comptime if be all comptime?

tacit hull
#

it would

cursive tusk
#

yeah, which is what the original question is asking about

stoic magnet
#

I thought it was asking about conditional runtime code

cursive tusk
#

just look at the g'damn question

stoic magnet
#

ye, I see it...

gleaming pendant
#

Yeah I agree with Chuck's reading

tacit hull
#

same

#

but use whatever works for you

#

that's the gist of it

gleaming pendant
#

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 comptime keyword, which is what corner suggested (although he used unnecessary parens, comptime isn't a function: just if (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 to inline functions do get comptime-evaluated; or rather, the function's body is effectively pasted straight into the condition and evaluated there)
cursive tusk
#

just right click the question on the left and mark as "closed"

#

if you don't, eventually somebody on the mod team will

tacit hull
#

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

cursive tusk
#

should be defined as having the lowest precedence

tacit hull
cursive tusk
#

lol

tacit hull
#

so that I can do try f() orelse a

cursive tusk
#

anyway, end of discussion, question is answered

tacit hull
#

because today it's try (f() orelse a)

#

yes