Why can I write
val flat: List[Int] = List(Some(1), Some(2)).flatMap(i => i)
But not
val flat: List[Int] = List(Some(1), Some(2)).flatMap(_)
See Question&Answers more detail:osWhy can I write
val flat: List[Int] = List(Some(1), Some(2)).flatMap(i => i)
But not
val flat: List[Int] = List(Some(1), Some(2)).flatMap(_)
See Question&Answers more detail:osThe underscore is a placeholder for a variable. It's not, in and of itself, a function. flatMap
requires a function.
Also, in this case, there is a flatten
method which is defined on List
for which Option
has within the Predef
an implicit conversion such that what you've written can be condensed into just that call, List(Some(1)).flatten