I cannot figure out how the Scala compiler determines how to use flatMapwith the Options sequence .
If I use flatMapfor sequence sequences:
println(Seq(Seq(1), Seq()).flatMap(a => a)) // List(1)
it will concatenate all nested sequences
The same thing happens if I use it with the sequence Options:
println(Seq(Some(1), None).flatMap(a => a)) // List(1)
So, it is flatMapconsidered Optionas a collection in this case. The question is, why does this work? flatMaphas the following definition:
def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That
The value that the function that returns the instance expects GenTraversableOncebut Optiondoes not inherit GenTraversableOnce. He inherits only Productand Serializable, and Productinherits Equals.
Scala flatMap Option ?