Hacker News new | ask | show | jobs
by mgaunard 768 days 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.

6 comments

You'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.
>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.

Tagged unions + pattern matching is what gp wants. You can always encode whatever model you want using any programming language, but language features/ergonomics matter.
That 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.
lol this is like saying C doesn't need structs, you can just declare the variables with a common prefix separately! See ma, product types!
Yep, 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!
That doesn't work since you cannot pass that as a single argument to a function.
So? make the function accept multiple arguments?
There are restrictions on calling conventions; it's not equivalent.
That is not a proper alternative to real pattern matching.