Sealed classes
Traits and classes can be marked sealed which means all subtypes must be declared in the same file. This assures that all subtypes are known.1
2
3
4
5
6
7
8sealed abstract class Furniture
case class Couch() extends Furniture
case class Chair() extends Furniture
def findPlaceToSit(piece: Furniture): String = piece match {
case a: Couch => "Lie on the couch"
case b: Chair => "Sit on the chair"
}
This is useful for pattern matching because we don’t need a “catch all” case.
more >>