Union type structs

In my project I have a Value union(enum). I know you can do a Entry.zig that has top level fields, and you can import it as a type of that name, but this is because it is implicitly an struct, how can I do this for union(enum)?

You can’t, instead you need to do something like const Entry = @import("entry.zig").Entry if Entry needs to be something that isn’t a default struct.
So you have to declare it as a non-top-level container type.

Thank you, do you think this will change in the future, because the documentation says:
“If the file (implicitly a struct) has top level fields, it should be named like any other struct with fields using TitleCase”
I didn’t know if you could explicitly choose something else besides a struct somehow, but I guess you can’t.

I don’t think so, since it would require changing the language parsing/grammar. Since structs are already containers, then files fit that model. Enums and unions can be containers, but then it is ambuguous whether a file is a struct or an enum. There is no way to tell.
For example, if we were to have the file be an enum or union, is the following a struct, an enum, or a union?

this: u8,
that: u8,
another: SomeType,

The unified body parsing for these container makes it hard to tell. You would have to have some kind of hint or something.

Structs make the most sense since they are the more expected behavior.
There has been other discussion along this line:


Disclaimer: I am not a compiler dev, all opionions are my own.

A corollary of that is that if the file does not have top level fields, the file should be named as a namespace using snake_case.

edit: corrected for clarity, thanks pzittlau

Sorry to be that guy but: snake_case.