logoalt Hacker News

ginkoyesterday at 12:53 PM1 replyview on HN

Zig's inline for is also evaluated at comptime:

https://ziglang.org/documentation/master/#inline-for


Replies

samatmanyesterday at 8:31 PM

Well yes, but the _effect_ is to unroll the loop for runtime, if the inline-for survives that long.

A for loop executed during comptime is just

    const stuff = comptime stuff: {
       for (0...8) |i| {
         // etc, build up some stuff
       }
       break :stuff some_stuff;
    };
The difference is that a comptime block won't leave behind runnable 'residue', only whatever data is constructed for later. An inline for might not leave behind an unrolled loop either, but it can.