13
は動作します:Optionはforループ内で明示的にtoListを必要とするのはなぜですか?簡単なオプションでループのために使用
scala> for (lst <- Some(List(1,2,3))) yield lst
res68: Option[List[Int]] = Some(List(1, 2, 3))
しかし、オプションの内容をループしません:
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)
なぜ明示的なリストの変換が必要とされていますか?これは慣用的な解決策ですか?
だと思います - いくつかの(一覧(1,2,3))を取得; X < - オプション(LST ))yield x'も働く。面白い。 – sberry