5 ms·
ADTs and pattern matching go together. Having tagged unions is awful without a good way to discriminate upon them. Switch statements are ok but not typesafe s
by nimish 12y ago
ADTs and pattern matching go together. Having tagged unions is awful without a good way to discriminate upon them.
Switch statements are ok but not typesafe since nothing prevents you from desynchronizing the tag and data
- tel 12y agoIf you think of ADT definitions as defining how you construct types then you get a natural method for destructing them or using them automatically. This could be pattern matching or it could be explicit functions More concretely, even without pattern matching, so long as, for instance, these three functions are all defined pair : a -> b -> Pair a b pi1 : Pair a b -> a pi2 : Pair a b -> b You'll be equivalently expressive.
- taejo 12y agoC already has product types, so it's sums that are more interesting, and indeed the destructors for sums are harder in C. The constructors in1 : a -> Sum a b in2 : b -> Sum a b are no problem, but the destructor sum : Sum a b -> (a -> c) -> (b -> c) -> c needs first-class functions.
- deleted 12y ago[deleted]
- dllthomas 12y agoC already has sum types, just slightly sloppy ones (as unions aren't required to have a tag).
- tel 12y agoThere's a difference between "having a destructor" and having a first-class representation of it. For instance, basically every language has a destructor for booleans—it's just if. The trick is that unless you have first-class functions you probably cannot reify such a destructor. It always just lives in your syntax. On the flip side, that's really what it means for a language to, say, lack sum types. It's not that the language doesn't have the ability to have choice, it's that it lacks the ability to represent choice in data. In other words, you probably can eliminate choice using if/switch/case/what-have-you but you can't construct it.