Type Classes
Type classes are used to enable ad-hoc polymorphism, aka overloading.
OO uses subtyping for polymorphism usually. FP uses a combination of parametric polymorphism and ad-hoc polymorphism.
A type class:
- Declares functionality. (Traits)
- Guarantees behavior. (Laws)
In order to learn about a type class, you have to learn these two characteristics. (Laws are especially easy to ignore. Don't.)
Remember this is polymorphism.
A type class doesn't necessarily define the trait. There can be multiple implementations of the trait, as long as it follows the laws.
That being said, sometimes laws force a specific definition for a function. (E.g.
map
method forMonad
is defined in terms offlatMap
andpure
)
Data Types and Type Classes
Type classes are used to enable data types to have polymorphism.
Data types have a "has-a" relationship with type classes, not a "is-a" relationship.
- [Correct] Data type
A
has an instance of type classB
- [Correct] Data type
A
has 5 different instances of type classB
- [Wrong] Data type
A
is a type classB
- [Correct]
Either
has an instance of aMonad
. - [Wrong]
Either
is aMonad
.