0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Exhaustive Matching

Compiler-checked completeness.

Exhaustive Matching is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 4 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.

What Is Exhaustive Matching?

A match is exhaustive when it handles every possible value of the type being matched.

For a sealed trait, the compiler knows all variants and can check that your match covers them all.

A Complete Match

When you handle every variant of a sealed trait, the compiler is satisfied and no warning appears.

sealed trait Color
case object Red extends Color
case object Green extends Color
case object Blue extends Color
object Main {
  def name(c: Color): String = c match {
    case Red   => "red"
    case Green => "green"
    case Blue  => "blue"
  }
  def main(args: Array[String]): Unit = {
    println(name(Blue))
  }
}

The Compiler Has Your Back

If you leave out a variant, the compiler emits a non-exhaustive match warning at compile time.

This is a safety net: you find out about the gap before running the program.

Adding a Variant Later

The real payoff comes when you add a new variant. Every non-exhaustive match across your codebase lights up with a warning, reminding you to handle the new case.

This makes refactoring safe and predictable.

Why Avoid a Wildcard

Adding case _ => silences exhaustiveness warnings. That can hide bugs when you add variants later.

For sealed ADTs, prefer listing each variant explicitly so the compiler keeps protecting you.

sealed trait Status
case object Active extends Status
case object Inactive extends Status
object Main {
  def label(s: Status): String = s match {
    case Active   => "on"
    case Inactive => "off"
  }
  def main(args: Array[String]): Unit = {
    println(label(Active))
  }
}

Exhaustiveness With Data

Exhaustiveness applies to variants carrying data too. You bind the fields and must still cover every variant.

sealed trait Shape
case class Circle(r: Double) extends Shape
case class Square(s: Double) extends Shape
object Main {
  def area(sh: Shape): Double = sh match {
    case Circle(r) => 3.14159 * r * r
    case Square(s) => s * s
  }
  def main(args: Array[String]): Unit = {
    println(area(Circle(2)))
    println(area(Square(3)))
  }
}

Combining With Guards

Be careful: guards can make a match non-exhaustive because the compiler cannot prove the guards cover all values.

Provide an unguarded final case for the same variant to stay exhaustive.

sealed trait Temp
case class Celsius(v: Int) extends Temp
object Main {
  def feel(t: Temp): String = t match {
    case Celsius(v) if v >= 30 => "hot"
    case Celsius(v)            => "not hot"
  }
  def main(args: Array[String]): Unit = {
    println(feel(Celsius(35)))
    println(feel(Celsius(10)))
  }
}

Exhaustiveness With Option

The standard library's Option is a sealed ADT with Some and None. Matching both makes it exhaustive.

object Main {
  def show(o: Option[Int]): String = o match {
    case Some(v) => s"value $v"
    case None    => "nothing"
  }
  def main(args: Array[String]): Unit = {
    println(show(Some(7)))
    println(show(None))
  }
}

Nested Exhaustiveness

Exhaustiveness also helps with nested ADTs. The compiler reasons about each level you destructure.

sealed trait Tree
case object Leaf extends Tree
case class Node(v: Int, l: Tree, r: Tree) extends Tree
object Main {
  def depth(t: Tree): Int = t match {
    case Leaf          => 0
    case Node(_, l, r) => 1 + math.max(depth(l), depth(r))
  }
  def main(args: Array[String]): Unit = {
    println(depth(Node(1, Leaf, Node(2, Leaf, Leaf))))
  }
}

Why It Matters

Compiler-checked exhaustiveness gives you:

  • Confidence that every case is handled
  • Automatic reminders when the ADT grows
  • Fewer runtime MatchErrors
  • Safer large-scale refactoring

Putting It Together

A sealed ADT with full coverage. Adding a new variant would trigger a compiler warning here.

sealed trait Command
case object Start extends Command
case object Stop extends Command
case class SetSpeed(v: Int) extends Command
object Main {
  def run(c: Command): String = c match {
    case Start       => "starting"
    case Stop        => "stopping"
    case SetSpeed(v) => s"speed $v"
  }
  def main(args: Array[String]): Unit = {
    List(Start, SetSpeed(5), Stop).foreach(c => println(run(c)))
  }
}

Quick Check

Test your understanding of exhaustive matching.

Recap

You learned about exhaustive matching:

  • A match is exhaustive when it covers every variant
  • For sealed traits the compiler checks this and warns on gaps
  • Adding a variant surfaces every incomplete match
  • Avoid case _ on sealed ADTs to keep that protection
  • Guards can break exhaustiveness; add an unguarded fallback

Frequently asked questions

Is the “Exhaustive Matching” lesson free?

Yes — the full text of “Exhaustive Matching” 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 “Exhaustive Matching”?

Compiler-checked completeness. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Exhaustive Matching” 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

  1. Case Classes
  2. Sealed Traits
  3. Algebraic Data Types
  4. Exhaustive Matching
← Back to Scala for Backend Engineering & Functional Programming