Can arguments in Zig be lazy evaluated?

It looks the answer is no. But I’m not sure if there is a way.

const print = @import("std").debug.print;

// Arbuments passed to v will always be evaluated,
// regardless of what the value b is, even if it is comptime.
inline fn foo(b: bool, v: u8) u8 {
    if (b) return v;
    return 0;
}

fn bar() u8 {
    print("bar\n", .{}); // being printed
    return 1;
}

pub fn main() void {
    const x = foo(false, bar());
    print("x = {}\n", .{x});
}

Nope, arguments are evaluated before the call is even evaluated.

The compiler (optimiser) could remove them after the fact, but since bar prints that won’t happen.

1 Like