Why it matters

Type classes underlie most Scala libraries. Understanding shapes library design + use.

Advertisement

The architecture

Trait: Show[A] with 'def show(a: A): String'.

Instances: implicit vals per type.

Use: 'def print[A](a: A)(implicit s: Show[A])'.

Type class patternType class traitShow[A]Instancesimplicit valsConsumerimplicit paramCats provides standard type classes: Functor, Monad, Semigroup, Monoid, etc.
Type class components.
Advertisement

How it works end to end

Derivation: automatic instance generation for case classes (Circe, ScalaCheck).

Scala 3: 'given' keyword replaces implicit. Cleaner.

Coherence: only one instance per type + type class.