Implicit conversion is not applied if a method has

2020-06-04 04:16发布

问题:

The question is based on the discussion here. This is the setup:

implicit def CToC2(obj: C1): C2 = {
  new C2()
}

class C1() {
  def f[U](f: (Int, Int) => U): U = f(1, 1)
}

class C2() {
  def f[U](f: ((Int, Int)) => U): U = f(2, 2)
}

I'd expect that trying to call the function with a signature that exists in C2, scala would use the implicit conversion to satisfy the call:

val c1 = new C1()
val ff: ((Int, Int)) => Unit = t => println(t._1 + t._2)

But this fails:

scala> c1.f(ff)
Error:(16, 7) type mismatch;
 found   : ((Int, Int)) => Unit
 required: (Int, Int) => ?

Interestingly if I drop the type parameter from C1, it works fine:

class C1() {
  def f(f: (Int, Int) => Unit): Unit = f(1, 1)
}