Scala 中 final class 和 sealed class 有什么区别?

What are the differences between final class and sealed class in Scala?

Scala中有两种修饰符:finalsealed

它们之间有什么区别?什么时候应该使用一个而不是另一个?

A final class 无法延期,句号。

sealed 特性只能在声明的同一个源文件 中扩展。这对于创建 ADT(代数数据类型)很有用。 ADT 由其派生类型的 sum 定义。

例如:

  • Option[A]Some[A] + None 定义。
  • A List[A]:: + Nil 定义。

sealed trait Option[+A]

final case class Some[+A] extends Option[A]
object None extends Option[Nothing]

因为 Option[A] 是密封的,它不能被其他开发者扩展 - 这样做会改变它的 含义

Some[A] 是最终的,因为它不能被延长,期间。


作为一个额外的好处,如果特征被密封,如果你的模式匹配不够详尽,编译器会警告你,因为它知道Option有限SomeNone.

opt match {
    case Some(a) => "hello"
}

Warning: match may not be exhaustive. It would fail on the following input: None

sealed classes(或traits)在同一个源文件中仍然可以被继承(其中final classes根本不能被继承) .

当您想限制一个碱基 class 的子 class 数量时使用 sealed(参见 "Algebraic Data Type")。

作为这种限制的一个非常实际的好处,编译器现在可以警告您非穷尽模式匹配:

sealed trait Duo
case class One(i:Int) extends Duo
case class Two(i:Int, j:Int) extends Duo

def test(d:Duo) {
  match {
    case One(x) => println(x) // warning since you are not matching Two
  }
}