13
使用for循环用一个简单的选件上工作:为什么Option在循环内需要显式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)
为什么需要显式列表转换?这是惯用的解决方案吗?
这就是为什么'的(LST < - 一些(名单(1,2,3))获得; X < - 选项(LST ))产量x'也有效。有趣。 – sberry