Scala 言語仕様のセクション6.19には次のように書かれています。
理解のためのA
for (p <- e) yield e0
はに翻訳されますe.map { case p => e0 }
そう...
scala> val l : List[Either[String, Int]] = List(Left("Bad"), Right(1))
l: List[Either[String,Int]] = List(Left(Bad), Right(1))
scala> for (Left(x) <- l) yield x
res5: List[String] = List(Bad)
ここまでは順調ですね:
scala> l.map { case Left(x) => x }
<console>:13: warning: match is not exhaustive!
missing combination Right
l.map { case Left(x) => x }
^
scala.MatchError: Right(1)
at $anonfun$1.apply(<console>:13)
at ...
2 番目のバージョンが機能しないのはなぜですか? というか、最初のバージョンが機能するのはなぜですか?