`inline for` behaves unexpectedly when calling a function

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

You need to call the function at comptime

1 Like

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

1 Like

huh??? i don’t get this.

1 Like

The question or the solution?