Implicit conversion is not applied if the method has a parameter of type

The question is based on a discussion here . This is the setting:

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 expect that trying to call a function with a signature that exists in C2 , scala will use an 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) } 
+5
source share

Source: https://habr.com/ru/post/1266716/


All Articles