Re:Show HN: Sum (algebraic) types for C in one 100 line header
by Hirrolot on Tuesday May 28, 2024 @11:36AM
(#40501791)
I used to define `Maybe` in C as a macro over a tagged union. It really unveiled a number of logic errors at compile-time, but the issue of course is to use this kind of metaprogramming sanely. E.g., instantiating `Maybe` to some other macro `T(a)`, where `a` is some other (concrete) type, would already cause a headache.
Take one of the most basic sum types in Haskell's base library, Maybe:
This simple construction gives you the ability to write functions over optional values and avoid the issues with null pointers. This would be an amazing feature to have in C but it can't be achieved due to C's very limited type system.