#Define an array of structs that is not only available at comptime but without using a block

1 messages · Page 1 of 1 (latest)

spice charm
#

I was trying to make a map of strings to Zig enums and I found two ways.

One:

var BUILTINS = .{
    .{ .name = "SELECT", .kind = Token.Kind.select_keyword },
    .{ .name = "SELECT", .kind = Token.Kind.select_keyword },
    ... more stuff ...
};

But this I can only seem to iterate on with for (BUILTINS) |builtin| {...} as a comptime thing.

Then I had this other version

var BUILTINS = block: {
    var builtins: [13]Builtin = undefined;
    builtins[0] = .{ .name = "SELECT", .kind = Token.Kind.select_keyword };
    builtins[1] = .{ .name = "CREATE", .kind = Token.Kind.create_keyword };
  
    ... more stuff ...

    break :block builtins;
};

This second version is not restricted to comptime. But it's more convoluted to type.

Is there a version of the former one where I construct a literal array of anonymous structs but that isn't limited to comptime looping?

Thanks!

glacial cloak
#

var BUILTINS: [_]Builtin = .{

#

i think that works

spice charm
#

I was hoping not to need to define Builtin as a struct. Is that possible while avoiding it needing to be comptime only?

#

oh duh, I already had that defined. Trying, your suggestion

glacial cloak
#

it's var BUILTINS = [_]Builtin{ mb

spice charm
#

yeah that works, thank you!

spice charm
#

no worries, I already had Builtin defined. I just forgot