Common Type Classes
Show, Ordering, Numeric.
Common Type Classes is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)
}Custom Ordering
Pass an explicit Ordering to sort differently, for example descending or by a derived key.
@main def run(): Unit = {
val nums = List(3, 1, 2)
println(nums.sorted(Ordering.Int.reverse))
val words = List("bbb", "a", "cc")
println(words.sortBy(_.length))
}Ordering for Case Classes
Provide an Ordering instance for your own type so collections of it can be sorted. Ordering.by derives one from a key.
case class Person(name: String, age: Int)
object Main {
implicit val byAge: Ordering[Person] = Ordering.by(_.age)
def main(args: Array[String]): Unit = {
val people = List(Person("A", 30), Person("B", 20))
println(people.sorted)
}
}Numeric Abstracts Arithmetic
Numeric[A] lets you write functions that work over any number type. sum on a list relies on it.
@main def run(): Unit = {
println(List(1, 2, 3).sum)
println(List(1.5, 2.5).sum)
}Writing Generic Numeric Code
With a context bound [A: Numeric] you can add or multiply any number type generically. Use the instance's operations via Numeric[A].
object Main {
def addAll[A: Numeric](xs: List[A]): A = {
val num = implicitly[Numeric[A]]
xs.foldLeft(num.zero)(num.plus)
}
def main(args: Array[String]): Unit = {
println(addAll(List(1, 2, 3)))
println(addAll(List(1.0, 2.0, 3.5)))
}
}Numeric Operators
Importing the instance's Implicits gives you the usual operators (+, *) on generic numeric types, making code read naturally.
object Main {
def square[A](a: A)(implicit num: Numeric[A]): A = {
import num._
a * a
}
def main(args: Array[String]): Unit = {
println(square(5))
println(square(2.5))
}
}A Show Type Class
Show renders values as text in a type-safe way (unlike toString, which exists on everything). Cats provides it; here is a minimal version.
trait Show[A] { def show(a: A): String }
object Show {
implicit val intShow: Show[Int] = n => s"Int($n)"
def apply[A](implicit s: Show[A]): Show[A] = s
}
object Main {
def main(args: Array[String]): Unit = {
println(Show[Int].show(42))
}
}Ordering and max/min
Methods like max, min, and maxBy on collections all use Ordering under the hood.
@main def run(): Unit = {
val xs = List(4, 9, 1, 7)
println(xs.max)
println(xs.min)
println(List("a", "abc", "ab").maxBy(_.length))
}Combining Orderings
You can build composite orderings with Ordering.by on a tuple, sorting by one field then another.
case class Rec(group: String, score: Int)
object Main {
implicit val ord: Ordering[Rec] = Ordering.by(r => (r.group, -r.score))
def main(args: Array[String]): Unit = {
val data = List(Rec("b", 5), Rec("a", 1), Rec("a", 9))
data.sorted.foreach(println)
}
}Why Type Classes Here
Because comparison and arithmetic are type classes, the same sorted or sum works for built-in types and your own, simply by supplying an instance. This is the standard library using ad-hoc polymorphism.
Quick Check
Test your knowledge of standard type classes.
Recap
You met common standard type classes:
Ordering[A]drivessorted,max,min; build withOrdering.by.Numeric[A]enables generic arithmetic and powerssum.Show-style classes render values type-safely.
Frequently asked questions
Is the “Common Type Classes” lesson free?
Yes — the full text of “Common Type Classes” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “Common Type Classes”?
Show, Ordering, Numeric. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Common Type Classes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Type Class Pattern
- Defining Instances
- Common Type Classes
- Type Class Derivation