In trying to figure out the configuration for zift I wanted to have the indent field be just a non exhaustive enum, with the tabs value being 0, and other value being spaces. However, apparently simply typing the number in the zon like
.{
.indent = 4,
}
for example, doesn’t work.
Is there a way to do something like this or does it have to be a union(enum) ?
For it to have any chance of working. I don’t know if Zon accepts this, however, it would diverge from Zig if it allowed an integer to cast straight to an enum like that.
It is probably better in this case but I think there is use of integer fields that can have some tagged values. The example coming to mind right now is font weight (for a future zig editor or w/e), where the user either writes 350 or .regular.
Tho in that case probably just use a proper config language.
Yea, I think I’m the context of configuration this makes sense, but for the language as a whole it’s syntactic sugar that probably wouldn’t break the threshold for being worth it for the core team to implement.
That said, I’m pretty you could get the behavior you’re after by using a custom parsing function on that enum.
To find this, I looked at zig std and searched for zon. In there, I looked for the relevant functions, then used the [src] link to read the code, and dug through a few functions until I found the relevant sections. I’ve found the files in codeberg for purposes of permalinks to the relevant lines.
I haven’t touched this stuff yet but just chiming in with my two cents: I think if your type is a nonexhaustive enum (which therefore has a backing integer type), it should succeed when the value is a raw integer that fits in the backing integer type. Same goes for packed structs.