Why is the comptime keyword needed in this inline for loop condition?

One more condition: a function will also be implicitly executed at comptime if it is in a comptime position. For example:

pub fn main() void {
    var arr: [add(20, 400)]u8 = undefined;
    _ = &arr;
}

pub fn add(a: i32, b: i32) i32 {
    return a + b;
}

This compiles fine (tested on 0.16.0-dev.2296+fd3657bf8). The array length is required to be comptime-known, and so add(20, 400) is executed at comptime.

I think this previous comment of mine would help shed some light onto the semantics of inline for, which will help explain why inline for doesn’t force the value of the condition to be known at comptime, like the above array length example does.

2 Likes