Search code examples
scalaimplicit-conversioniterable

Scala Option[String] map turns in Iterable


Why does this compile?

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

Solution

  • 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).