0PricingLogin
Scala for Backend Engineering & Functional Programming · Lesson

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

  1. The Type Class Pattern
  2. Defining Instances
  3. Common Type Classes
  4. Type Class Derivation
← Back to Scala for Backend Engineering & Functional Programming