Creating a slice of StructField at comptime

I want to collect a struct’s fields into a slice, excluding the field called “positional” if it exists.

    const flag_fields = comptime blk: {
        const StructField = std.builtin.Type.StructField;
        var fields: []StructField = &[_]StructField{};
        for (std.meta.fields(Flags)) |field| {
            if (std.mem.eql(u8, field.name, "positional")) continue;
            fields = fields ++ &[1]StructField{field};
        }
        break :blk fields;
    };

This does not work:

src/core.zig:102:29: error: expected type '[]builtin.Type.StructField', found '*const [1]builtin.Type.StructField'
            fields = fields ++ @constCast(&[1]StructField{field});
                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/core.zig:102:29: note: cast discards const qualifier

How else can I achieve this?

Works for me if you add const on the slice:

var fields: []const StructField = &[_]StructField{};
1 Like

Silly me. Thank you.

1 Like