class Foo
implicit def fromInt[A <% Int](x: A) = new Foo // #1
implicit def fromString[A <% String](x: A) = new Foo // #2
0: Foo
给出
error: type mismatch;
found : Int(0)
required: this.Foo
Note that implicit conversions are not applicable because they are ambiguous:
both method fromInt of type [A](x: A)(implicit evidence$1: A => Int)this.Foo
and method fromString of type [A](x: A)(implicit evidence$2: A => String)this.Foo
are possible conversion functions from Int(0) to this.Foo
0: Foo
^
我不确定我理解。
如果我删除了#2,它就会编译。
如果删除#1,则无法编译
error: No implicit view available from Int => String.
0: Foo
^
没有歧义。 fromString
不从Int
到Foo
的转换函数。
为什么编译器声称存在歧义?