1
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
。
爲什麼編譯器聲稱有歧義?