How do I call a method on or access a value of an argument wildcard? Eg in this example I want to find the max "rev" value of all F objects.
scala> case class F(rev:Long)
defined class F
scala> List(F(1),F(2),F(3))
res3: List[F] = List(F(1), F(2), F(3))
scala> res3.foldLeft(0L){math.max(_,_.rev)}
<console>:11: error: wrong number of parameters; expected = 2
res3.foldLeft(0L){math.max(_,_.rev)}
^
You can't use wildcards here and need to give names to the arguments:
res3.foldLeft(0L){(x,y) => math.max(x,y.rev)}
Note that it would be the same if you had a function foo
taking 1 argument instead of math.max
: foo(_.rev)
is the same as foo(x => x.rev)
and not x => foo(x.rev)
.
The problem is the scope of wildcards. math.max(_,_.rev)
expands to (I think) x => math.max(x, y => y.rev)
. Since this function only has one parameter, you get this error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.