For exploratory purposes, I want to write some code, and make sure it isn’t evaluated at compile time. What’s the best way to achieve that? Right now, I am trying
I’m not 100% sure, but I think that if the arg is a var, it can’t be evaluated at compile time. So
var n: i32 = 92;
f(n);
should do the trick? I arrive at this conclusion given that if you try to use a var i instead of a comptime var i with an inline while you get an error stating the i can’t be evaluated at compile time.
The rule right now is that a function is called at comptime implicitly only when you’re already in a comptime context, otherwise it will need to be prefixed with comptime. There has been discussion about eventually have the compiler “try” to resolve function calls at comptime without explicitly asking for it, but none of that exists today.
So a function call in the top scope of a file is always comptime, while a function call inside main is always at runtime unless prefixed with comptime.
Not sure how that interacts with LLVM optimizations though, so you might get a non-comptime compile time resolution by LLVM when the function is simple enough.
This can be used to provide alternative, comptime-friendly implementations of functions. It should not be used, for instance, to exclude certain functions from being evaluated at comptime.
Yes, and thank you for pointing out that those do have warnings attached to them (probably should have added that). @matklad seems like the kind of programmer that will dig into implementations to find out what the rules are, but for the general case it’s good to follow the warnings.
Hi, just thought I’d quickly note: that note in @inComptime’s documentation isn’t any kind of technical restriction, it’s more of a recommendation. One of the main concerns with implementing that builtin was that people would start needlessly (and potentially wrongly) writing things like this:
if (@inComptime()) @compileError("This function must be run at runtime");
I chose to add this note simply to make it clear that this isn’t recommended. There’s no weird behavior here; the builtin does indeed just return whether it was evaluated in a comptime scope or not.