I might be missing something but I came across a pattern that surprisingly doesn't work.
Here it is:
object A {
def bar(func: (Int, Int) => Int): Int = 2
def bar(func: Int => Int): Int = 3
def foo(func: Int => Int): Int = 4
}
def f(n: Int) : Int = n + 1
val g: Int => Int = f
A.foo(f) // works fine
A.bar(f) // doesn't work
A.bar(g) // but this works
The compiler ask me to explicitly apply the method f
in order to pass it (writing f _
) :
Unapplied methods are only converted to functions when a function type is expected.
I don't get why the conversion is implicitly made when passing f
to A.foo
but not when passed to A.bar
. It might be related to the fact that bar
has two overloads but I'm not sure why ?
I'm using scalac with Scala 2.12.8.