The Type Class Pattern
Ad-hoc polymorphism.
What is a Type Class?
A type class is a pattern for adding behavior to types without modifying them. It is a form of ad-hoc polymorphism: the same operation works for many unrelated types, chosen by the compiler based on type.
It comes from Haskell and is widely used in Scala libraries like Cats.
The Problem It Solves
Inheritance forces behavior into a type at definition time. But you often cannot edit a type (it is in a library), or you want different behaviors in different contexts. Type classes let you attach behavior externally.
All lessons in this course
- The Type Class Pattern
- Defining Instances
- Common Type Classes
- Type Class Derivation