3 ms·
How does it compare to the tagged unions in Zig, which aims to be “C with the bugs fixed”?
by geon 6y ago
How does it compare to the tagged unions in Zig, which aims to be “C with the bugs fixed”?
- flohofwoe 6y agoZig has the advantage that it can add the syntax sugar right into the language instead of using macro "workarounds", but I'd guess in the end both have similar lowlevel memory layout (a struct with an enum tag, followed by a payload "raw union") and generated code (e.g. it would be interesting to look at the compiler outputs, they should be very similar).
- eqvinox 6y agoThis "advantage" is also its greatest disadvantage. I can stick these macro definitions in any existing project and start incrementally rolling them out. Migrating something — even partially — to Zig is a much bigger task.
- Hirrolot 6y agoHmm, I don't use Zig so can't say much about it.
- flohofwoe 6y agoThis is what it looks like in Zig: https://ziglang.org/documentation/master/#Tagged-union https://ziglang.org/documentation/master/#Tagged-union You create two types, a tag enum, and the tagged union itself, which has a typed "payload" for each tag. Plus the necessary "syntax sugar" for initialization and extracting the payload by tag.