Reputation: 44937
Consider the following code snippet:
def foo(a: Int)(b: Int) = a + b
foo
It does not compile, and produces the following error message:
error: missing argument list for method foo
Unapplied methods are only converted to functions when
a function type is expected.
You can make this conversion explicit by writing
`foo _` or `foo(_)(_)` instead of `foo`.
The foo _
hint works. But if I write the expression
foo(_)(_)
as suggested by the previous error message, I get a new error message:
error: missing parameter type for expanded
function ((x$1: <error>, x$2: <error>) => foo(x$1)(x$2))
This seems rather counterintuitive.
Under what circumstances is the foo(_)(_)
-hint supposed to be helpful, what exactly does it tell me?
(noise removed; the more I kept editing the question, the less sense did it make; Kolmar is right)
Upvotes: 1
Views: 799
Reputation: 14224
The type of foo(_)(_)
is (Int, Int) => Int
. So it works if you specify that type or use it in the context that expects this type:
scala> foo(_: Int)(_: Int)
res1: (Int, Int) => Int = $$Lambda$1120/1321433666@798b36fd
scala> val f: (Int, Int) => Int = foo(_)(_)
f: (Int, Int) => Int = $$Lambda$1121/1281445260@2ae4c424
scala> def bar(f: (Int, Int) => Int): Int = f(10, 20)
bar: (f: (Int, Int) => Int)Int
scala> bar(foo(_)(_))
res2: Int = 30
Upvotes: 2