logoalt Hacker News

boomlindetoday at 7:57 AM0 repliesview on HN

> What might not be clear about this is that a struct loses runtime information. If you want to iterate a map, that's easy: call for (key, val) in map { .... If you want to iterate a struct ... get fucked? write a proc-macro?

I don't see how the example would benefit at all from having that information at run-time when it exists at compile-time. The problem with Rust specifically I think is that it's a relative PITA to get to the information that exists at compile-time in this case. In Zig, you'd just iterate over the fields using the same language you use for run-time code.

    const fields = std.meta.fields(@TypeOf(instance));
    inline for (fields) |field| {
        const name = field.name;
        const value = @field(instance, name);
        std.debug.print("{s}: {any}", .{name, value};
    }
The combination of high dynamicity at compile-time with low-effort switching between compile-time and run-time contexts makes this not too bad.