I have two definition of foo, and one of them is more supposedly specific
def foo(f: java.util.function.ToIntFunction[String]) = println("foo1")
def foo[T](f: String=>T) = println("foo2")
//def foo[T](f: String=>T)(implicit d: DummyImplicit) = println("foo2") //Does not work either
foo({_: String => 1}) //Should pick foo1, but gives error instead
Error is:
error: ambiguous reference to overloaded definition,
both method foo in object Main of type [T](f: String => T)Unit
and method foo in object Main of type (f: java.util.function.ToIntFunction[String])Unit
I also tried the trick of DummyImplicit but it still gives the same error. How can I achieve compile time overload when Int presents without using reflection?
I am using Scala 2.12 with SAM type support.
Edit
I hope to get a solution that is not limited to using Java converters, because ToIntFunction interface can be replaced by Scala trait, e.g.
trait ToIntFunction[T] { def apply(v: T): Int }
def foo(f: ToIntFunction[String]) = println("foo1")
def foo[T](f: String=>T) = println("foo2")
as I think it is a more generic problem for method overload.
On my machine with Scala 2.12
foo({_: String => 1})evaluates tofoo2, thus I am unable to reproduce the issue. My interpretation of SAM conversion in overloading resolution in Scala 2.12 is thatFunction-typed arguments take precedence, thus it should evaluate tofoo2:Note
_: String => 1isFunction1. To force it to evaluate tofoo1tryasJavafrom scala-java8-compat like so:As per Krzysztof Atłasik's comment it is reproducible in Scala 2.13.