我们假设如下: class Wrapper1 { case class Condition(test: String)}object Wrapper1 extends Wrapper1class Wrapper2 { case class Condition[A](test: String)}object Wrapper2 extends Wrapper2class Test type T = // whatever def test(fn: T
class Wrapper1 {
case class Condition(test: String)
}
object Wrapper1 extends Wrapper1
class Wrapper2 {
case class Condition[A](test: String)
}
object Wrapper2 extends Wrapper2
class Test
type T = // whatever
def test(fn: T => Wrapper1.Condition): X
def test[R](fn: T => Wrapper2.Condition[R]): X
}
问题是由于类型擦除,这些方法在擦除后具有完全相同的类型.可以很容易地改变第二个的签名:
def test[R](fn: T => Wrapper2.Condition[R])(implicit ev: R =:= R): X
但这会混淆编译器并在其他地方使用测试方法是不可能的.出于多种设计原因,我试图保持这种方法的名称一致.有没有办法成功地做到这一点?
似乎这不是 Scala double definition (2 methods have the same type erasure)的副本我的建议……只需要一个方法
def test[Cond: TypeTag](fn: T => Cond): X = {
if(typeOf[T] <:< typeOf[Wrapper1.Condition]) ...
else if(typeOf[T] <:< typeOf[Wrapper2.Condition[_]) ...
else ...
}
