4 ms·
Type classes are a completely different concept. Tagged unions are sum types.
by greydius 9y ago
Type classes are a completely different concept. Tagged unions are sum types.
- deleted 9y ago[deleted]
- k__ 9y agoI see. Care to explain what type classes are then? :)
- tome 9y agoA "type class" specifies an interface and you can make types into "instances" of a "type class" by saying how it implements that interface.
- comex 9y agoThey’re known in other languages as interfaces, traits, or protocols: just a set of methods which can be implemented for different types. Importantly, it’s possible to implement type classes for existing types in a separate declaration, as opposed to traditional OO languages where a list of implemented interfaces has to be declared upfront as part of the class declaration. Thus, for instance, a client of a library can declare its own type class and implement it for the library’s types. (Many languages other than Haskell support this too, though.)
- k__ 9y agoSo they're a bit like open classes in Ruby or implicit conversions in Scala?
- lmm 9y agoThey achieve the same result, but in a quite different way. I see them as decoupling the "implements X" piece of a traditional OO class in a language that has interfaces from the class itself.
- jnbiche 9y agoIf you're familiar with Scala, then type classes in Haskell are pretty close to traits in Scala (but not implicit conversions, which can make use of traits, but are an orthogonal language feature). They're also close to Rust's traits, it you're familiar with Rust.
- k__ 9y agoI see. Thanks.