Reputation: 10429
The Scala docs say the flatten api flattens a list of lists and it can only be invoked on a list of lists.
Why is it possible to invoke it on the following then?
List(Some("Tony"), None).flatten
Upvotes: 3
Views: 686
Reputation: 32335
The ScalaDoc API shows the [use case]
-- a simplified representation of the method signature.
If you click on the Full signature
it will expand into the complete signature:
Full Signature
def flatten[B](implicit asTraversable: (A) ⇒ GenTraversableOnce[B]): List[B]
You can call flatten
on lists, or on most other collections for that matter, as long as there is an implicit conversion from the list element type A
(in your case Option[String]
) to a traversable of any other type.
Any Option
type can be implicitly converted into a GenTraversableOnce
-- Some
acts as a single element collection and None
as an empty collection.
This means that you can call flatten
on the List[Option[String]]
to obtain a List[String]
.
Upvotes: 10