Scala Option [String]地图变为Iterable

问题描述 投票:3回答:1

为什么这个编译:

def foo() : Iterable[URI] = {
   Some("")
     .map(URI.create)
}
scala option implicit-conversion iterable
1个回答
4
投票

implicit conversion called option2Iterable上直接定义了Option,它将所有Option[A]转换为零或一个元素的Iterable[A]

一个更短的代码片段,演示了这种有些意外的行为如下:

(Option(42): Iterable[Int])

它将悄然将Option转换为List,在这种情况下生产List(42)

© www.soinside.com 2019 - 2024. All rights reserved.