3 ms·
Zig has tagged unions and the corresponding switch expressions, is that not equivalent?
by c-cube 5y ago
Zig has tagged unions and the corresponding switch expressions, is that not equivalent?
- skohan 5y agoI was not aware of these, either it was added since I kicked the tires on Zig or I just wasn't aware of it, but yes it looks pretty good! The one drawback I see is that it seems a bit verbose: i.e. the tag set itself has to be declared as a separate enum, and then the tags need to be repeated inside the union. So it looks to be slightly bolted-on and unergonomic (similar to TypeScript's implementation) but I haven't worked with it so maybe I am missing something.
- nyberg 5y agoYou can use union(enum) to avoid having to write a separate enum definition for tags and std.meta.FieldEnum() exists should you also want to derive an enum from the union later on.