Scala Option[String] map turns in Iterable

Viewed 598

Why does this compile?

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

There is an implicit conversion called option2Iterable defined directly on Option, it converts all Option[A] into an Iterable[A] with zero or one element.

A much shorter code snippet that demonstrates this somewhat unexpected behavior is the following:

(Option(42): Iterable[Int])

It will quietly convert Option into a List, in this case producing a List(42).

Related