Why does this Scala placeholder not work -
this question has answer here:
- scala foreach strange behaviour 5 answers
given these case classes:
case class featuredistance(id: long, distance: double) case class searchresult(score: float, id: long)
why not compile?
val distances = list[featuredistance](featuredistance(1l, 10f)) val results = distances.map(searchresult(0f, _.id))
but does:
val results = distances.map(fd => searchresult(0f, fd.id))
the compilation error says: missing parameter type expanded function ((x$3) => x$3.id)
is because _
scoped map function it's not visible in searchresult.apply
call?
after doing bit of research, found post on old scala forums contains quote:
when use "_" place holder anonymous parameter of function, scope of function innermost parenthesis containing it.
so, it's question of scope. suspect has problems otherwise result having nested function calls use more 1 underscore. instance:
//suppose have x:list[list[int]] x.map(_.map(_ + 1))
Comments
Post a Comment