假设代码:
class A
class B
class Something {
def method(arg: A) = ???
def method(arg: B) = ???
}
class C
object C {
implicit def ctoa(c: C): A = ???
implicit def ctob(c: C): B = ???
}
另外:
-
类
A
,B
和Something
及其随播广告无法修改 -
必须有从
C
到A
以及从C
到B
的隐式转换,它们的优先级无关紧要 -
除此之外,类
C
及其伴星可以自由修改 -
当然,可以添加更多类型,含义等
现在,有没有办法进行编译:
(new Something).method(new C)
?
1 回答
我知道这不是你想要的,但我没有看到任何方法去做,除了将其中一个暗示到另一个范围 .
然后你得到:
否则,您试图违反非歧义规则:"An implicit conversion is only inserted if there is no other possible conversion to insert."请参阅Programming in Scala .