The problem here is not a type inference.
As you can see from error message equivalent code for s.map(_)
is not s.map(x => x)
, but this:
i => s.map(i)
Just like print(_)
(actually Predef.print(_)
) means i => Predef.print(i)
.
Or like "a" + _
means "a".+(_)
means s => "a".+(s)
.
This just doesn't make sense in current context.
Let's suppose you have a list of functions String => String
(fs
) and you want to apply all these functions to list of String
. You'll use this code:
fs.map{f => s.map(f)}
or just this:
fs.map{s.map(_)}
Addition: you could use identity
method instead if x => x
. It's imported by default, but you could make it even shorter using addition import:
import Predef.{identity => id}
List(1, 2, 3) map id
// List[Int] = List(1, 2, 3)
Note that identity
is a well known name, so in team you should use it instead of your aliases.