6 ms·
I'm convinced that lack of Sum Types like this in languages like Java/C#/Go are one of the key reasons that people prefer dynamic languages. It's incredibly fre
by nicoburns 6y ago
I'm convinced that lack of Sum Types like this in languages like Java/C#/Go are one of the key reasons that people prefer dynamic languages. It's incredibly freeing to be able to express "or". I do it all the time in JavaScript (variables in dynamic languages are basically one giant enum of every possible value), and I feel incredibly restricted when using a language that requires a class hierarchy to express this basic concept.
- dragonwriter 6y ago> I'm convinced that lack of Sum Types like this in languages like Java/C#/Go are one of the key reasons that people prefer dynamic languages. It doesn't hurt that static languages (TypeScript) or tools (mypy) that lightly lay on top of dynamic languages often do support sum types.
- fgonzag 6y agoI completely agree. Every passing day I become more convinced that a statically typed language without sum types or more broadly ADTs is fundamentally incomplete. The good news is that many languages seem to be cozying up to them, and both the JVM (through Kotlin, Scala, et all) and .net (through F# or C# w/ language-ext) support them. Even better news is that the C# team has stated that they want to implement Sum Types and ADTs into the language, and are actively looking into it.
- wvenable 6y agoI just don't see, in properly designed code, that there would be that much use for sum types if you have generics. When are you creating functions take or return radically different types that need to be expressed this way? I dislike dynamic languages where parameters and variables can take on any type -- it's rarely the case that same variable/parameter would ever need to contain a string, a number, or a Widget in the same block of code. I find it much more freeing to have the compiler be in charge of exactness so I can make whatever changes I need knowing that entire classes of mistakes are now impossible.
- deleted 6y ago[deleted]
- Ar-Curunir 6y agoRust has both generics and sun types, and benefits enormously from both. And sum types aren’t for “radically different types”. You can define an error type to be one of different options (I.e. a more principled error code), or to represent nullability in the type system, or to indicate fallibility without relying on exceptions, etc. Rust uses all of these to great effect, and does so because these sum types are generic.
- GolDDranks 6y agoBecause even with generics, you are not able to express "or"; two different choices of types that have _different_ APIs. With generics, you can express n different choices of types that have all the _same_ API. It's a good software engineering principle to make control and data flow as streamlined as possible, for similar data. Minimize branches and special cases. Generics help with this, they hide the "irrelevant" differences, surfacing only the relevant. On the other hand, if there are _actually_ different cases, that need to be handled differently, you want to branch and you want to express that there are multiple choices. Sum types make this a compiler-checked type system feature.
- deleted 6y ago[deleted]
- coolreader18 6y agoLet's take Rust's hash map entry api[0], for example. How would you represent the return type of `.entry()` using only a class hierarchy? let v = match map.entry(key) { Entry::Occupied(o) => { o.get_mut() += 1; o.into_mut() } Entry::Vacant(v) => { update_vacant_count(v.key()); v.insert(0) } }; I view sum types as enabling the exact same exactness as you describe in your last line; especially since you can easily switch/match based on a specific subtype if you realize you need that, without adding another method to the base class and copying into the x subclasses that you have for implementing the different behavior. [0]: https://doc.rust-lang.org/std/collections/hash_map/enum.Entry.html https://doc.rust-lang.org/std/collections/hash_map/enum.Entr...