# \`inline for\` behaves unexpectedly when calling a function

**URL:** <https://ziggit.dev/t/inline-for-behaves-unexpectedly-when-calling-a-function/16326>\
**Category:** Help\
**Tags:** comptime\
**Created:** [June 24, 2026, 3:59pm UTC](https://ziggit.dev/t/inline-for-behaves-unexpectedly-when-calling-a-function/16326 "2026-06-24T15:59:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shrimo](https://ziggit.dev/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@Shrimo](https://ziggit.dev/u/Shrimo)\
**Post date:** [June 24, 2026, 3:59pm UTC](https://ziggit.dev/t/inline-for-behaves-unexpectedly-when-calling-a-function/16326/1 "2026-06-24T15:59:09Z")

</div>

```zig
fn is_even(n: usize) bool {
    return (n % 2 == 0);
}

pub fn main() void {
    const numbers = .{ 1, 2, 3, 4, 5, 6 };

    inline for (numbers) |n|
        if (n % 2 == 0) @compileLog(n); // 2, 4, 6

    inline for (numbers) |n|
        if (is_even(n)) @compileLog(n); // 1, 2, 3, 4, 5, 6
}

```

The first loop logs `2, 4, 6`, which is expected.  
The second logs `1, 2, 3, 4, 5, 6`.

Why does wrapping the predicate in a function change the behaviour? I would’ve expected it to be evaluated given `n` is comptime known.

Version: `0.17.0-dev.956+2dca73595`

---

<div class="post-metadata">

**Author:** ![SillyProgramming](https://ziggit.dev/user_avatar/ziggit.dev/sillyprogramming/32/8444_2.png) [@SillyProgramming](https://ziggit.dev/u/SillyProgramming)\
**Post date:** [June 24, 2026, 4:01pm UTC](https://ziggit.dev/t/inline-for-behaves-unexpectedly-when-calling-a-function/16326/2 "2026-06-24T16:01:38Z")

</div>

You need to call the function at comptime

---

<div class="post-metadata">

**Author:** ![rpkak](https://ziggit.dev/letter_avatar_proxy/v4/letter/r/a4c791/32.png) [@rpkak](https://ziggit.dev/u/rpkak)\
**Post date:** [June 24, 2026, 4:04pm UTC](https://ziggit.dev/t/inline-for-behaves-unexpectedly-when-calling-a-function/16326/3 "2026-06-24T16:04:16Z")

</div>

The `is_even()` function call is evaluated at runtime. Because of this, the compiler evaluates the code, for both possibilities of the return value of `is_even()`. You get 2, 4, 6 if you do `if (comptime is_even(n)) @compileLog(n);`.

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [June 24, 2026, 5:51pm UTC](https://ziggit.dev/t/inline-for-behaves-unexpectedly-when-calling-a-function/16326/4 "2026-06-24T17:51:09Z")

</div>

huh??? i don’t get this.

---

<div class="post-metadata">

**Author:** ![Shrimo](https://ziggit.dev/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@Shrimo](https://ziggit.dev/u/Shrimo)\
**Post date:** [June 24, 2026, 6:15pm UTC](https://ziggit.dev/t/inline-for-behaves-unexpectedly-when-calling-a-function/16326/5 "2026-06-24T18:15:22Z")

</div>

The question or the solution?

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [June 24, 2026, 10:24pm UTC](https://ziggit.dev/t/inline-for-behaves-unexpectedly-when-calling-a-function/16326/6 "2026-06-24T22:24:40Z")

</div>

the solution

---

<div class="post-metadata">

**Author:** ![LucasSantos91](https://ziggit.dev/user_avatar/ziggit.dev/lucassantos91/32/440_2.png) [@LucasSantos91](https://ziggit.dev/u/LucasSantos91)\
**Post date:** [June 25, 2026, 2:18am UTC](https://ziggit.dev/t/inline-for-behaves-unexpectedly-when-calling-a-function/16326/7 "2026-06-25T02:18:12Z")

</div>

Just manually unroll the loop and it will make sense.

```zig
pub fn main() void {
    const numbers = .{ 1, 2, 3, 4, 5, 6 };

    // First loop
    if (1 % 2 == 0) {
        // Known at compile time to be false,
        // so the compiler doesn't even look
        // at what's inside here.
        // The compiler never reaches this
        // `compileLog`, so the 1 doesn't get logged.
        @compileLog(1); 
    }
    if (2 % 2 == 0) {
         //Known at compile time to be true,
         // so the block gets compiled, and the compiler
         // sees the `compileLog` and logs 2
         @compileLog(2); 
    //...

    // Second loop
        if (is_even(1)) {
             // Not known at compile time, so the compiler
             // must compile what's inside, and it
             // reaches the `compileLog`.
             // Even if this branch gets removed
             // by optimizations, the logging has 
             // already been done.
            @compileLog(1);
        }
    // Same for all other elements, so all get logged
}

```

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [June 25, 2026, 6:12am UTC](https://ziggit.dev/t/inline-for-behaves-unexpectedly-when-calling-a-function/16326/8 "2026-06-25T06:12:53Z")

</div>

ok… it is some `@compilelog` / compiletime thingy. not a logical thing.  
if we replace @compilelog with print we get the same output for both loops.

and… but… still don’t get it! my brain refuses to understand what is happening.

---

<div class="post-metadata">

**Author:** ![ForeverZer0](https://ziggit.dev/user_avatar/ziggit.dev/foreverzer0/32/2443_2.png) [@ForeverZer0](https://ziggit.dev/u/ForeverZer0)\
**Post date:** [June 25, 2026, 6:22am UTC](https://ziggit.dev/t/inline-for-behaves-unexpectedly-when-calling-a-function/16326/9 "2026-06-25T06:22:21Z")

</div>

Dead code elimination.

```zig
    inline for (numbers) |n|
        if (n % 2 == 0) @compileLog(n); // 2, 4, 6

```

This code is being _evaluated_ at comptime. It can detect the dead branches, and they are eliminated. The inner block is never even seen by the compiler. The `@compileLog` is never even reached by the compiler.

```zig
    inline for (numbers) |n|
        if (is_even(n)) @compileLog(n); // 1, 2, 3, 4, 5, 6

```

This is not being evaluated at comptime, therefore the compiler goes through the normal processing of the branches. It is not dead code that gets eliminated at the `if`, so the `@compileLog` is reached for each occurrence due to the loop being unrolled with `inline for`.

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [June 25, 2026, 6:25am UTC](https://ziggit.dev/t/inline-for-behaves-unexpectedly-when-calling-a-function/16326/10 "2026-06-25T06:25:19Z")

</div>

i will play around with this and watch what is happening. then maybe some neurons will click 🙂
