4 ms·
If only all languages had sum types …
by apgwoz 3y ago
If only all languages had sum types …
- contravariant 3y agoYou can make one in most languages with polymorphic types. I mean the sum type A+B is by definition just a type with maps A => A+B, B => A+B and for each two functions A => X and B => X a function A+B => X compatible with both maps. Even if the language doesn't support it it's easy enough to make one. Syntax is nicer with language support though.
- apgwoz 3y agoWe went in a circle. The article does this. The parent comment says “use sum types.” I said “most languages don’t have direct support” and now you’re repeating the article.
- contravariant 3y agoBest I can tell the article does something else, though the notion that you can just implement the language features you lack is the same.
- apgwoz 3y agoYou can definitely implement language features poorly and in hard to maintain, “against the grain” ways in most languages. Lots of languages support multiple ways, sometimes, of doing so!
- deredede 3y agoImplementing sum types in a language that doesn't support them is so common that it has a name: the visitor pattern. Somewhat interestingly the visitor pattern is even useful in languages that have sum types as it makes it easier to implement generic traversals!
- contravariant 3y agoI wish having a polymorphic return value was more standard though. It always feels iffy to make a visitor that needs to keep an internal state in order to return anything.