【发布时间】:2014-02-15 07:14:42
【问题描述】:
本示例的设置(Scala 2.10.3):
trait S[A]
trait T[A]
implicit class X[A : S](a: A) { def foo() { } }
implicit class Y[A : T](a: A) { def foo() { } }
implicit object I extends S[String]
这样编译:
new X("").foo()
这不是:
new Y("").foo()
因为没有隐含的T[String]。
could not find implicit value for evidence parameter of type T[String]
new Y("").foo()
^
因此,我假设 scalac 可以明确地应用从 String 到 X 的隐式转换:
"".foo()
但是我们得到:
type mismatch;
found : String("")
required: ?{def foo: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method X of type [A](a: A)(implicit evidence$1: S[A])X[A]
and method Y of type [A](a: A)(implicit evidence$1: T[A])Y[A]
are possible conversion functions from String("") to ?{def foo: ?}
"".foo()
^
这是故意的吗? scalac 在枚举候选者时是否不应该考虑每次转换是否真的有效?
【问题讨论】: