0PricingLogin
Scala for Backend Engineering & Functional Programming · Lesson

Common Type Classes

Show, Ordering, Numeric.

Standard Type Classes

Scala's standard library ships several type classes you use daily, sometimes without realizing it:

  • Ordering[A] — how to compare values.
  • Numeric[A] — arithmetic over numbers.
  • Show-style rendering (custom, or Cats' Show).

Ordering Powers sorted

List.sorted needs an implicit Ordering[A]. The standard library provides instances for numbers, strings, and more.

@main def run(): Unit = {
  println(List(3, 1, 2).sorted)
  println(List("pear", "apple", "fig").sorted)
}

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