Alternatives to operator overloading: infix functions

Far from the main point of this thread, but @offset you have mentioned that comptime for is used for loop unrolling several times, but that is not true. comptime evaluates the given expression entirely at compile time. It does not unroll loops. The inline keyword is used to unroll loops. Consider this snippet:

comptime var x: comptime_int = 0;
comptime for (0..10) |i| {
    x = i + x * 2;
};
inline for (0..5) |_| {
    std.debug.print("x is {}\n", .{x});
}

For both kinds of loops, the number of iteration must be known at compile time. Both loops could loop over an array with a comptime known value, but an inline for can also loop over an array (note: comptime known length) with a runtime known value. The body of a comptime loop can only operate on comptime data, like I am demonstrating by mutating a comptime var. The body of an inline for loop can do runtime operations, which I am demonstrating by doing IO.

6 Likes