Вопрос

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?

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top