Sealed hierarchies
A sealed trait may only be extended in the file where it is declared. That one restriction is what lets the compiler know every case of the type: a match that leaves one out is incomplete, and the compiler says so before the program runs. A sealed hierarchy is the way a Scala program writes a closed set of alternatives that each carry their own data.
A closed set of cases
Expr is a number, a sum or a product. Each case is a case class that extends the trait, and eval recurses over the tree without a default branch, because there is no other case to handle:
expr.scala
sealed trait Expr
case class Num(value: Int) extends Expr
case class Sum(left: Expr, right: Expr) extends Expr
case class Product(left: Expr, right: Expr) extends Expr
def eval(expr: Expr): Int = expr match
case Num(value) => value
case Sum(left, right) => eval(left) + eval(right)
case Product(left, right) => eval(left) * eval(right)
def render(expr: Expr): String = expr match
case Num(value) => value.toString
case Sum(left, right) => s"(${render(left)} + ${render(right)})"
case Product(left, right) => s"(${render(left)} * ${render(right)})"
@main def sealed_hierarchies(): Unit =
val expr = Sum(Num(1), Product(Num(2), Num(3)))
println(render(expr))
println(eval(expr))
scala-cli run --server=false expr.scala
(1 + (2 * 3))
7
Both methods are total, and neither has a case _ that would hide a case added later. That is the point of sealed: when a fourth case joins the file, every match over the type becomes a compile error until it is handled, which is a to-do list the compiler writes.
The warning for a missing case
A match that forgets a case still compiles, but the compiler warns about it and names the case it would fail on. Triangle is declared and never matched here:
missing.scala
sealed trait Shape
case class Circle(radius: Double) extends Shape
case class Square(side: Double) extends Shape
case class Triangle(base: Double, height: Double) extends Shape
def area(shape: Shape): Double = shape match
case Circle(radius) => math.Pi * radius * radius
case Square(side) => side * side
@main def missing(): Unit =
println(area(Circle(1.0)))
mkdir -p out
scalac -color:never -d out missing.scala 2>&1; echo "exit status: $?"
-- [E029] Pattern Match Exhaustivity Warning: missing.scala:6:33 ---------------
6 |def area(shape: Shape): Double = shape match
| ^^^^^
| match may not be exhaustive.
|
| It would fail on pattern case: Triangle(_, _)
|
| longer explanation available when compiling with `-explain`
1 warning found
exit status: 0
The warning names the pattern that would fail, Triangle(_, _), and the file still compiles, which is why the warning is easy to miss in a long build. A program turns it into an error with -Werror, and then a new case cannot be forgotten at all.
Related
- Enums: the shorter spelling of a closed set of cases.
- Pattern matching: the match this lesson leans on.
- Inheritance and abstract members: sealed beside final.
- Option, Either and Try: three sealed hierarchies of the standard library.
Автор статьи: Arthur Isaev