In this section, we'll be exploring ad hoc polymorphism, but through the usage of type classes.
In computer science, polymorphism is the provision of a single interface to entities of different types. Polymorphism consists of three types: subtyping, parametric polymorphism, and ad hoc polymorphism.
Subtyping enables polymorphism by having different implementations of the same method (but keeping the interface) in the different subclasses. Parametric polymorphism enables polymorphism by allowing code to be written without the mention of a specific type. For example, when you operate over a generic
List
, you're applying parametric polymorphism. Ad hoc polymorphism enables polymorphism by allowing different and heterogeneous implementations
depending on specified types. Method overloading is an example
of ad hoc polymorphism.