Scala Option[String] 映射变成 Iterable

Scala Option[String] map turns in Iterable

为什么编译:

def foo() : Iterable[URI] = {
   Some("")
     .map(URI.create)
}

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

下面是一个更短的代码片段,它演示了这种有点意外的行为:

(Option(42): Iterable[Int])

它将悄悄地将 Option 转换为 List,在本例中生成 List(42)