【发布时间】:2018-07-16 12:00:55
【问题描述】:
为什么会这样:
val x: Seq[Any] = Vector.empty
x match {
case Nil => 1
case _ => 2
}
Vector.empty 内部等同于:
private[immutable] val NIL = new Vector[Nothing](0, 0, 0)
override def empty[A]: Vector[A] = NIL
匹配Nil 并返回1? Nil 不只是Seq 的特定子类型吗?
如果我使用更通用的Seq.empty,答案将保持不变。这是为什么呢?
【问题讨论】:
-
我认为这是因为
Vector[Nothing]和Nil是AbstractSeq[Nothing]的实例。Seq.empty也有同样的类型。
标签: scala collections pattern-matching hierarchical