The problem is that you want to flatMap a List[Option[T]]
to a List[T]
:
val l = List(Some("Hello"), None, Some("World"))
to get:
List(Hello, World)
but there is no nice solution:
l flatMap( o => o)
l flatMap identity[Option[String]]
l flatMap ( x => Option.option2Iterable(identity(x)))
for(x <- l; y <- x) yield y
The obvious solution using the identity function does not work, due to a needed type conversion from Option[T]
to Iterable[T]
:
l flatMap identity
<console>:6: error: type mismatch;
found : A
required: Iterable[?]
l flatMap identity
Is there a way around this problem?
Part of the question is why does the type inferencer work differently if implicit type conversions are needed?
(This question came up when this question about the identity function was discussed.)