Since their inception in Haskell, type classes have become a popular feature of modern functional programming. Indeed, key topics in FP often take the form of type classes, such as Monoid and Functor. Many languages developed since Haskell have included type classes in their design, such as Idris and Purescript; or offered facilities capable of simulating type classes, such as Scala's implicits or OCaml's modules.

In this talk we go beyond the basics with type classes. First we'll differentiate between parametric and ad hoc polymorphism, and we'll see why type classes are a compelling solution to the latter. By comparing them to other solutions - Java interfaces and Scala implicits - we'll see the advantages of type classes. In particular, we will focus on why type classes are principled. This section will include examples in Java, Scala, and Haskell.

In order to be principled, there are conditions on the creation of instances: there can be a maximum of one instance for a given type for a given type class, and it must be in one of two allowed places. We will justify this rigidity by discussing instance coherence and its benefits, with examples of incoherence silently causing bugs. We will learn what orphan instances are and why they can be problematic for coherence.

Finally, practical guidelines will be given for programmers aiming to simulate type classes in other languages, as is popular in the Scala community. These guidelines, when followed with discipline, will allow one to recover many of the benefits of type classes. Examples in this section will be in Scala.

Goal

Attendees will leave knowing the difference between parametric and ad hoc polymorphism, and they will understand why type classes are a principled approach to the latter. Attendees wishing to simulate type classes in a capable language, such as Scala, will leave the talk with practical guidelines helping them to do so.