In Scala 2.8, flatten will work:
Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val listOfOptions = List(None, Some("hi"), None)
listOfOptions: List[Option[java.lang.String]] = List(None, Some(hi), None)
scala> listOfOptions flatten
res0: List[java.lang.String] = List(hi)
This doesn't work in 2.7.7, however:
Welcome to Scala version 2.7.7.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20).
scala> val listOfOptions = List(None, Some("hi"), None)
listOfOptions: List[Option[java.lang.String]] = List(None, Some(hi), None)
scala> listOfOptions.flatten
:6: error: no implicit argument matching parameter type (Option[java.lang.String]) => Iterable[Nothing] was found.
listOfOptions.flatten
The collections library has been redesigned, and has improved a lot in 2.8, so perhaps you might want to try to use the latest Scala 2.8 RC and see if that makes it more easy to use for you.
If you really don't want to use the toList method, I guess you can also write it like this:
scala> listOfOptions.flatMap(o => o)
res: List[java.lang.String] = List(hi)
Also not a thing of beauty perhaps, but at least this works in 2.7.7.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…