There’s @enumToInt builtin for (infailably) converting an enum value to underlying int. Is there something to go in the opposite direction (which naturally needs to be able to fail). I use the following code, is there anything better?
const Hand = enum {
rock, paper, scissors,
fn from_int(val: u8) ?Hand {
inline for (@typeInfo(Hand).Enum.fields) |field, i| {
if (i == val) return @field(Hand, field.name);
}
return null;
}
};
$ /opt/zig-0.10/zig build-exe itoe.zig
itoe.zig:11:15: error: enum 'itoe.Enum' has no tag with value '3'
const y = @intToEnum(Enum, x);
^~~~~~~~~~~~~~~~~~~
itoe.zig:4:14: note: enum declared here
const Enum = enum {
@intToEnum was renamed to @enumFromInt, and it was changed to not explicitly take the enum type as a parameter (instead inferring it from the use site). You can write something like const x: MyEnum = @enumFromInt(int_val).
If you want to catch invalid values, then the std.meta function you found is indeed the thing to use.