1

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)

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93

1 Answers1

2

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
Kolmar
  • 14,086
  • 1
  • 22
  • 25
  • Yes, thank you very much. I guess I just really managed to look at the error messages though the eyes of an unsuspecting novice while I was trying to understand [what exactly could be unclear about this syntax](https://stackoverflow.com/questions/52410976/what-does-an-underscore-after-a-scala-method-call-mean?noredirect=1). – Andrey Tyukin Sep 19 '18 at 20:07