I am confused. In TraversableLike, there is a function flatMap with the signature
flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): Iterable[B]
However, I can use it this way
scala> Iterable(1,2,3,4,5).flatMap{i=>if (i%2==0) {None} else {Some(i)}}
res1: Iterable[Int] = List(1, 3, 5)
Why is it possible? How is Option converted to GenTraversableOnce? It doesn’t seem like a subclass…
There is in fact an implicit conversion by default from Some[X] to GenTraversableOnce[X]. This is very simple to test in the REPL
And in fact this is defined in the object Option. Inside scala package:
option2Iterable is exactly what you are looking for. You can also see why when testing in your REPL you could see that the implementation of GenTraversableOnce is a list.
If you are looking for implicit conversions which are automatically imported without you doing anything ( like the one you can see in the REPL using implicitly) you have to look at: