【发布时间】:2011-10-22 00:39:19
【问题描述】:
List(1,2,3,4).sliding(2).map({ case List(a, b) => a < b }).forall(identity)
编译并返回true(尽管警告说匹配并不详尽)。
List(1,2,3,4).view
.sliding(2).map({ case List(a: Int, b: Int) => a < b }).forall(identity)
编译(只要我们包含 a 和 b 的类型注释)但会抛出 MatchError:
scala.MatchError: SeqViewC(...) (of class scala.collection.SeqViewLike$$anon$1)
at $anonfun$1.apply(<console>:12)
at $anonfun$1.apply(<console>:12)
at scala.collection.Iterator$$anon$19.next(Iterator.scala:335)
at scala.collection.Iterator$class.forall(Iterator.scala:663)
at scala.collection.Iterator$$anon$19.forall(Iterator.scala:333)
为什么?
【问题讨论】:
-
sliding返回一个Iterator,因此在view上调用它不会带来任何好处。
标签: scala view pattern-matching