# How to force runtime evaluation?

**URL:** <https://ziggit.dev/t/how-to-force-runtime-evaluation/943>\
**Category:** Help\
**Created:** [June 27, 2023, 10:54pm UTC](https://ziggit.dev/t/how-to-force-runtime-evaluation/943 "2023-06-27T22:54:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![matklad](https://ziggit.dev/user_avatar/ziggit.dev/matklad/32/2462_2.png) [@matklad](https://ziggit.dev/u/matklad)\
**Post date:** [June 27, 2023, 10:54pm UTC](https://ziggit.dev/t/how-to-force-runtime-evaluation/943/1 "2023-06-27T22:54:09Z")

</div>

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

```zig
pub fn main() { f(92) }
noinline fn f(arg: i32) void {}

```

where I think arg will be a runtime value.

Does this work? Is there a better way to do it?

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [June 27, 2023, 11:20pm UTC](https://ziggit.dev/t/how-to-force-runtime-evaluation/943/2 "2023-06-27T23:20:28Z")

</div>

I’m not 100% sure, but I think that if the arg is a `var`, it can’t be evaluated at compile time. So

```zig
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.

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [June 28, 2023, 4:03am UTC](https://ziggit.dev/t/how-to-force-runtime-evaluation/943/3 "2023-06-28T04:03:33Z")

</div>

Have you seen this thread? There was an @isComptime built in proposed a while ago: [Add `@inComptime` builtin · mlugg/zig@35d82d3 · GitHub](https://github.com/mlugg/zig/commit/35d82d31be3d2f2611049f41dc2616f898d70871)

I’m reading through the history of it right now and if I find out what happened to it I’ll post more here.

Here you can see something actually went in on this a while ago: [Add `@inComptime` builtin · mlugg/zig@35d82d3 · GitHub](https://github.com/mlugg/zig/commit/35d82d31be3d2f2611049f41dc2616f898d70871#diff-9674f03a76728eff712dfbd966487142bf4103b4a58049ee9696028e890f86bc)

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [June 28, 2023, 4:33am UTC](https://ziggit.dev/t/how-to-force-runtime-evaluation/943/4 "2023-06-28T04:33:23Z")

</div>

Yeah, so this works for me - maybe it’ll help you? zig version 0.11.0-dev.3867+ff37ccd29

```zig
fn foo() void {
    if (@inComptime()) {
        @compileError("Woops");
    }
}

pub fn main() !void 
{
    // no-compile error:
    foo();

    // compile error:
    comptime foo(); 
    
}

```

---

<div class="post-metadata">

**Author:** ![kristoff](https://ziggit.dev/user_avatar/ziggit.dev/kristoff/32/9_2.png) [@kristoff](https://ziggit.dev/u/kristoff)\
**Post date:** [June 28, 2023, 10:49am UTC](https://ziggit.dev/t/how-to-force-runtime-evaluation/943/5 "2023-06-28T10:49:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Damjan94](https://ziggit.dev/user_avatar/ziggit.dev/damjan94/32/580_2.png) [@Damjan94](https://ziggit.dev/u/Damjan94)\
**Post date:** [June 28, 2023, 1:06pm UTC](https://ziggit.dev/t/how-to-force-runtime-evaluation/943/6 "2023-06-28T13:06:55Z")

</div>

Thanks for the links, there seems to be a [warning](https://github.com/mlugg/zig/commit/35d82d31be3d2f2611049f41dc2616f898d70871#diff-9674f03a76728eff712dfbd966487142bf4103b4a58049ee9696028e890f86bcR8596)

> 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.

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [June 28, 2023, 3:25pm UTC](https://ziggit.dev/t/how-to-force-runtime-evaluation/943/7 "2023-06-28T15:25:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mlugg](https://ziggit.dev/user_avatar/ziggit.dev/mlugg/32/437_2.png) [@mlugg](https://ziggit.dev/u/mlugg)\
**Post date:** [July 1, 2023, 4:33pm UTC](https://ziggit.dev/t/how-to-force-runtime-evaluation/943/8 "2023-07-01T16:33:55Z")

</div>

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:

```zig
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.
