Struct Field Documentation (0.11.0)

I’d like to add one more piece here because comptime fields are actually quite powerful.

You can scan over them using @typeInfo in a loop and do some powerful reflection techniques with them. Here’s an example where I’m using it to run over auto-generated types to pickup if they have specific functions (those functions are comptime fields):

    const decls = CallbackType{};

    inline for (@typeInfo(CallbackType).Struct.fields) |field| {
        if (comptime reversePrefixed( {
            const edge_index = @field(decls,;

            if (last_edge <= edge_index) {
                const next_node = reverseEdge(
                // more stuff...

The thing is, if you’re using comptime fields, you probably have a specific use-case in mind and that struct is probably acting in a specialized way… here’s an example from another forum post we had on the same thing: How to iterate over struct declarations? - #3 by AndrewCodeDev

Just some food for thought. I wouldn’t rule them out, but they’re not as common, for sure.

I think what @Sze means to say is that they don’t have state like a normal field does. A comptime int isn’t stored in the same way that a runtime int is. If you look at their size, for instance with @sizeOf, you’ll see that it’s zero.

pub fn main() !void {
    const x: comptime_int = 1;
    std.debug.print("{}\n", .{ @sizeOf(@TypeOf(x)) }); // prints zero
1 Like