How can I cast EnumField

As a follow up of Iterating over a "packed" enum - #2 by quic5,

I want to use the builtin.Type.EnumField as the index of an array but can’t find the way to cast it

What I did is

inline for (std.meta.fields(Eum)) |field| {
    array[@intFromEnum(f)];
}

with the error

expected enum or tagged union, found ‘builtin.Type.EnumField’

Thanks,

Use std.enums.values(Eum) instead of std.meta.fields(Eum).

std.meta.fields gives you information about fields and not enum values as you want.

inline for (std.enums.values(Eum)) |value| {
    array[@intFromEnum(value)];
}
2 Likes

Perfect, thanks, I’m not yet familiar with the std

fyi: you don’t need an inline for

1 Like