3 ms·
These are great features, but they are really just syntactic sugar over algebraic types. It's not a more pleasant developer experience than sum (Option) types,
by davemp 8y ago
These are great features, but they are really just syntactic sugar over algebraic types. It's not a more pleasant developer experience than sum (Option) types, it's a more pleasant experience with sum types. Ex:
macro! unwrap(x, fallback) {
match x {
Some(n) => n,
None => fallback
};
}
> Typescript goes even further and has the best enumeration support I've seen any language have. T | U is a fully valid type, and if T | U is asserted to be one of them it is automatically cast to T/U. It is a very natural and efficient way of building ADTs
That's pretty cool. It seems like refinement types. [1]
[1]: https://en.wikipedia.org/wiki/Refinement_(computing)#Refinement_types https://en.wikipedia.org/wiki/Refinement_(computing)#Refinem...
- nostrademons 8y agoIt's subtly different because of the implicit coercions and smart casts. You can always pass T to a function that takes T?, while in Haskell/Rust you would need to pass Some(t). Similarly, Kotlin does control-flow analysis and converts all T?s inside an "if (t != null) ..." block or after an "if (t == null) return" statement into a T, which dramatically reduces the amount of try!/unwrap() calls that I used to see littering early Rust code. There's better syntactic sugar for it in Rust now, but the point is that Kotlin doesn't need nearly as much syntactic sugar because nullable is integrated with the typechecker and flow analysis.
- dtech 8y ago> It's not a more pleasant developer experience than sum (Option) types, it's a more pleasant experience with sum types. Having worked with both Scala, Haskel, Kotlin and Typescript I disagree, the dev experience for Kotlin/Typescript is miles ahead for optionality. It seems like a small difference vs something like do notation or for comphrehension but it really adds up. It hard to explain until you've tried it. The most succinct way would be to say that optional code looks and feels nearly identical to non-optional code, instead of unwrap/do-notation which makes a very large syntactical difference. Take a look at a piece of async Kotlin co-routine code v.s. Java/Scala (Completable)Future, same effect.