Implicit conversion is not applied if a method has type parameter
Asked Answered
M

0

6

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)
}
Marvellamarvellous answered 13/4, 2017 at 23:4 Comment(4)
More interesting is that you need to drop parameter even if it's not used, e.g. def f[U](f: (Int, Int) => Unit): Unit = f(1, 1) doesn't compile too.Goltz
You don't even have to make it that complicated. You get the same thing with def f[U](f: Int): Unit = ??? and def f(f: String): Unit = ???Ideography
github.com/scala/bug/issues/9523Ideography
there is now a PR with a fix for Scala 2.13Palate

© 2022 - 2024 — McMap. All rights reserved.