Hello,
overall the need for “bare enum” is rare in Zig, why do you need it?
These cases should cover all of usecases, since you should know the type of enum you want no?
If you are trying to create completely new enum type, the approach you chose thru @Type() is the best one.
If it is compile time known string of already existing enum type, you can use std.meta.stringToEnum, overall its nice to take a look inside the std.meta, there are many usefull things there.
If it is a runtime known string of already existing enum type, you can either make your own optimised comparing logic, or you can use static string map.
Thanks very much for the confirmation. I’d looked around and couldn’t find any previous solution for this.
I need it to convert field names (from std.meta.fields) into bare enums for an existing API that requires me to pass the field names as bare enums instead of strings.
These enums then get converted back into strings to be used as field names, but that part is beyond my control!
“bare enum” isn’t really a thing as far as I can tell and compared to a bare union it doesn’t seem like the same concept. Instead I would call this an adhocEnum or singletonEnum.
With “bare enum” I would expect a enum literal that doesn’t have any enum type associated with it, but that can’t be done for an arbitrary string at comptime, instead you would have to use the string to generate code at buildtime, if you wanted to create enum literals.
Because the enum value never changes we can just use @enumFromInt together with the type generated for the return type (which provides the name for this single enum value).