2014-09-21 68 views
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而不是轉換函數從IntFoo

爲什麼編譯器聲稱有歧義?

回答

相關問題