4 ms·
The sum type, which the author is talking about, is not the same as "discriminated union" pattern, the latter being the crude implementation of the former. It
by xyzzyz 9y ago
The sum type, which the author is talking about, is not the same as "discriminated union" pattern, the latter being the crude implementation of the former.
It really comes from functional languages like ML, and is extremely useful and convenient when combined with pattern matching. In fact, attempts to emulate it in OOP using interfaces and visitor pattern tend to bring a lot of boilerplate, and obscure the actual logic, which is exactly what the author is complaining about.
There's definitely more reason to use it than just "efficiency gain", which is why many languages introduced in the last decade have it built in (e.g. Rust, Scala or Swift).
- mcphage 9y ago> The sum type, which the author is talking about, is not the same as "discriminated union" pattern, the latter being the crude implementation of the former. Could you say something about the difference between the two? I'm not aware what it is. Edit: wow I accidentally a word.
- mrkgnao 9y agoIIRC discriminated unions are plain C(++) unions with an added type tag, like at the beginning of the article. Brittle, prone to breakage when refactoring, etc. when compared to real sum types.
- Koshkin 9y agoActually, the article says, "A sum type, also called a discriminated union..."