4 ms·
It's actually farther than Rust compared to Haskell.
by kaoD 7y ago
It's actually farther than Rust compared to Haskell.
- The_rationalist 7y agoWhy? It has union, intersection, recursive and conditional types.
- karavelov 7y agoNeither Haskell nor Rust have subtype polymorphism, TypeScript does. Haskell and Rust have type classes (traits), TypeScript does not. Etc.
- kaoD 7y agoJust to add since I find it an interesting nitpick: Rust has subtype polymorphism in lifetimes.
- kaoD 7y agoAdding to sibling comment: structural typing vs traits is kind of a game changer. The way I use them is very different. TS still feels like OOP (since it's just a formalization over JS's implicit native typing). Rust and Haskell feel mostly the same to me wrt how I think about my code (abstraction through capabilities). Intersection types are very different. In Haskell/Rust it means "this generic type implements traits A, B and C". In TS again it's about structure "it has at least the properties shared by types A, B and C". This means the intersection of A, B and C is all of A, B and C in Rust/Haskell, while in TS it can be a completely different type that only has their shared props, which makes no sense in Haskell/Rust since traits/typeclasses are disjoint (their intersection is always empty). All of them have recursive types (though in Rust the recursion must be boxed explicitly to make the type sized) and union types. Neither Rust nor Haskell have conditional types. This is really cool from TS: types are functions! Rust/Haskell are purely nominal.
- The_rationalist 7y agoInteresting, but I don't really get the difference about intersection types. (abstraction through capabilities) What differentiate an intersection type from a mixin? Secondly I get it that intersection types in typescript are more permissive but they are mostly functionaly the same, aren't they?
- kaoD 7y agoTraits are similar to mixins, this SE answer explains it better than I could: https://stackoverflow.com/questions/925609/mixins-vs-traits https://stackoverflow.com/questions/925609/mixins-vs-traits Sorry about the intersection confusion, I mixed up things. Intersection generic types are types that adhere to a set of traits (all of them, completely). Since traits don't overlap (even if their method names are the same) their intersection requires them to be defined explicitly. E.g. if you have an interface A { length: number } and an interface B { length: number, size: number } in Rust/Haskell you need to implement both explicitly for your type, while in TS your type just has to adhere to B's shape to be both. TL;DR: it's mostly about the "structural vs nominal" + "traits vs interfaces". But yeah they're functionally the same indeed, sorry for the confusion.