【发布时间】:2017-03-02 01:53:59
【问题描述】:
具体来说:
scala> def f(n: Seq[Any]) = n match {
case Nil => "Empty"
case h :: t => "Non-empty"
}
f: (n: Seq[Any])String
scala> f(Stream())
res1: String = Empty
scala> f(List(1))
res17: String = Non-empty
scala> f(Stream(1))
scala.MatchError: Stream(1, ?) (of class scala.collection.immutable.Stream$Cons)
at .f(<console>:13)
... 33 elided
还有很多其他方法可以实现这一点,但在编写代码时是静态安全的,并且在运行时会失败。怎么回事?
【问题讨论】:
标签: scala