此示例的设置(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
to的隐式转换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 在枚举候选人时是否不应该考虑每次转换是否真的有效?