4 ms·
C has always had them, it's called union. In practice you need to couple it with an enum, and your visitation mechanism is a switch statement. But C doesn't im
by mgaunard 2y ago
C has always had them, it's called union.
In practice you need to couple it with an enum, and your visitation mechanism is a switch statement. But C doesn't impose that on you and lets you do it as you see fit.
- duped 2y agoYou're confusing semantics for implementation. The point of union and discriminated union types (not what C calls union) is to enable compiler checked pattern matching, which tagged enums in C plus a switch statement do not get you.
- estebank 2y agoTagged unions + pattern matching is what gp wants. You can always encode whatever model you want using any programming language, but language features/ergonomics matter.
- coldtea 2y ago>C has always had them, it's called union It also has all the features of Haskell, since you can implement a Haskell compiler in C.
- naasking 2y agoThat you can sort of simulate the skeleton of algebraic data types does not mean that C has algebraic data types. The whole point of the algebra part is that the syntax has a compositional semantics which is completely absent in C, unless you go to great lengths as with this macro header.
- bmoxb 2y agoThat is not a proper alternative to real pattern matching.
- anon-3988 2y agolol this is like saying C doesn't need structs, you can just declare the variables with a common prefix separately! See ma, product types!
- grumpyprole 2y agoYep, or C doesn't need arrays just use pointers! And C doesn't need strings, just use a pointer to the first byte and assume it's ASCII!
- mgaunard 2y agoThat doesn't work since you cannot pass that as a single argument to a function.