Question

I am puzzled by the difference between

Array(true,false).filter(x=>x).map(println(_))

(runs fine)

and

Array(true,false).filter(_).map(println(_))

(throws error)

Notice the filter arguments: x=>x versus _. I was under the expression that x=>x and _ were synonymous. How to explain this?

Was it helpful?

Solution

filter(_) is desugared into x => filter(x). Look to your map usage: map(println(_)), it is desugared into map(x => println(x)) but not into map(println(x => x)), that not right and wont work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top