Paul Draper
Paul Draper

Reputation: 83323

Implicit conversions are not applicable because they are ambiguous

class Foo

implicit def fromInt[A <% Int](x: A) = new Foo       // #1
implicit def fromString[A <% String](x: A) = new Foo // #2

0: Foo

gives

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
^

I'm not sure I understand.

If I remove #2, it compiles.

If I remove #1, it fails to compile

error: No implicit view available from Int => String.
0: Foo
^

There is no ambiguity. fromString is not a conversion function from Int to Foo.

Why does the compiler claim that there is ambiguity?

Upvotes: 1

Views: 784

Answers (1)

Paul Draper
Paul Draper

Reputation: 83323

This appears to be a bug in 2.9.x and 2.10.x.

It is fixed in 2.11.2.

https://issues.scala-lang.org/browse/SI-8857

Upvotes: 1

Related Questions