Implicit conversion is not applied if a method has type parameter

大城市里の小女人 提交于 2019-12-04 13:45:46

问题


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)
}

来源:https://stackoverflow.com/questions/43402995/implicit-conversion-is-not-applied-if-a-method-has-type-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!