News for hackers. Stuff that matters.
Re:Algebraic Data Types for C99 by mgaunard on Thursday May 09, 2024 @10:51AM (#40308828)
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.

Re:Algebraic Data Types for C99 by duped on Thursday May 09, 2024 @11:04AM (#40308956)
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.
Re:Algebraic Data Types for C99 by coldtea on Thursday May 09, 2024 @11:22AM (#40309164)
>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.

Re:Algebraic Data Types for C99 by estebank on Thursday May 09, 2024 @11:05AM (#40308971)
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.
Re:Algebraic Data Types for C99 by naasking on Thursday May 09, 2024 @11:43AM (#40309389)
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.
Re:Algebraic Data Types for C99 by bmoxb on Thursday May 09, 2024 @12:03PM (#40309572)
That is not a proper alternative to real pattern matching.
Re:Algebraic Data Types for C99 by anon-3988 on Thursday May 09, 2024 @01:32PM (#40310623)
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!
Re:Algebraic Data Types for C99 by grumpyprole on Thursday May 09, 2024 @01:40PM (#40310712)
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!
Re:Algebraic Data Types for C99 by mgaunard on Friday May 10, 2024 @05:13AM (#40316847)
That doesn't work since you cannot pass that as a single argument to a function.
Re:Algebraic Data Types for C99 by anon-3988 on Friday May 10, 2024 @05:37AM (#40316981)
So? make the function accept multiple arguments?
Re:Algebraic Data Types for C99 by mgaunard on Saturday May 11, 2024 @05:00PM (#40330607)
There are restrictions on calling conventions; it's not equivalent.