単純な Option で for ループを使用すると、次のように機能します。
scala> for (lst <- Some(List(1,2,3))) yield lst
res68: Option[List[Int]] = Some(List(1, 2, 3))
ただし、 Option の内容をループしても、次のことは行われません。
scala> for (lst <- Some(List(1,2,3)); x <- lst) yield x
<console>:8: error: type mismatch;
found : List[Int]
required: Option[?]
for (lst <- Some(List(1,2,3)); x <- lst) yield x
^
...オプションが明示的にリストに変換されない限り:
scala> for (lst <- Some(List(1,2,3)).toList; x <- lst) yield x
res66: List[Int] = List(1, 2, 3)
なぜ明示的なリスト変換が必要なのですか? これは慣用的な解決策ですか?