When diving deeper into Scala I hit the term type class. It had been confusing because a class is a type and a type could be a class in Scala and "type" and "class" are in itself abstract terms.
After reading some examples I got a feeling what a type class could be, but maybe there is an easier approach to get a mental hook (picture, concept) to what a type class is by explaining why it had been named like that.
In computer science, a type class is a type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types.
A type class is an interface that defines some behavior. More specifically, a type class specifies a bunch of functions, and when we decide to make a type an instance of a type class, we define what those functions mean for that type.
Type classes are a powerful tool used in functional programming to enable ad-hoc polymorphism, more commonly known as overloading.
noun. a number of things or persons sharing a particular characteristic, or set of characteristics, that causes them to be regarded as a group, more or less precisely defined or designated; class; category. a criminal of the most vicious type.
In Haskell there are no classes in Java/Scala sense, so this term was free to be used (in the sense "class of types", as Johannes Weiß says).
It isn't used in official Scala documentation, but people coming from Haskell found they could emulate Haskell type classes in Scala and reused the name.
Because it classes (together) types "having" a common set operations defined by the type class?
It feels very natural to me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With