Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.9k views
Welcome To Ask or Share your Answers For Others

1 Answer

The 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...