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
- The Type Class Pattern
- Defining Instances
- Common Type Classes
- Type Class Derivation