3 ms·
Funny thing is, I am learning Zig at the moment and had to do just that and was impressed at how clean the solution was: const my_enum = enum { one
by steeve 3y ago
Funny thing is, I am learning Zig at the moment and had to do just that and was impressed at how clean the solution was:
const my_enum = enum {
one,
two,
three,
pub fn str(self: @This()) []const u8 {
return switch (self) {
inline else => |tag| @tagName(tag),
};
}
}
Inline else means the switch/case is expanded at compile time.
- winter_blue 3y agoWhy is the switch necessary here, if you only have an else clause for it? Couldn't you just return the tag expression itself?
- assbuttbuttass 3y agoThe "inline else" gets expanded at compile time into a separate case for each enumeration tag.
- amedvednikov 3y agoIn V it works automatically for all enums via a builtin str() method: my_enum.str()