Scala: strange behavior in `for` pattern matching for None case

久未见 提交于 2019-12-29 01:37:08

问题


Strange behavior in for cycle pattern matching:

scala> val a = Seq(Some(1), None)
a: Seq[Option[Int]] = List(Some(1), None)

scala> for (Some(x) <- a) { println(x) }
1

scala> for (None <- a) { println("none") }
none
none

Why in second example two output 'none' produced? Maybe this example is synthetic and not practical, but such behavior is not expectable. Is this bug or feature?


回答1:


What do you know, it is a bug:

https://issues.scala-lang.org/browse/SI-9324

scala> val vs = Seq(Some(1), None)
vs: Seq[Option[Int]] = List(Some(1), None)

scala> for (n @ None <- vs) println(n)
None

The spec in umambiguous:

http://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#for-comprehensions-and-for-loops

Compare midstream assignment, which does not exhibit the bug:

scala> for (v <- vs; None = v) println(v)
scala.MatchError: Some(1) (of class scala.Some)
  at $anonfun$1.apply(<console>:9)
  at $anonfun$1.apply(<console>:9)
  at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
  at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
  at scala.collection.immutable.List.foreach(List.scala:381)
  at scala.collection.TraversableLike$class.map(TraversableLike.scala:245)
  at scala.collection.immutable.List.map(List.scala:285)
  ... 33 elided



回答2:


That's because None was interpreted as a name of variable:

scala> for (None <- a) { println(None) } 
Some(1)
None

Here is simplified example without for:

scala> val None = 5
None: Int = 5

scala> val Some(a) = Some(5)
a: Int = 5


来源:https://stackoverflow.com/questions/33298595/scala-strange-behavior-in-for-pattern-matching-for-none-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!