title: Ambiguous implicit values
body: <p>I've been thinking I understand scala implicits until recently faced strange problem. </p>
  
  <p>In my application I have several domain classes</p>
  
  <pre><code>case class Foo(baz: String)
  case class Bar(baz: String)
  </code></pre>
  
  <p>And a class that is able to construct domain object from a string. It could be subclassed to do real deserialization it doesn't matter.</p>
  
  <pre><code>class Reads[A] {
    def read(s: String): A = throw new Exception("not implemented")
  }
  </code></pre>
  
  <p>Next, there are implicit deserializers</p>
  
  <pre><code>implicit val fooReads = new Reads[Foo]
  implicit val barReads = new Reads[Bar]
  </code></pre>
  
  <p>And a helper to convert strings to one of domain classes</p>
  
  <pre><code>def convert[A](s: String)(implicit reads: Reads[A]): A = reads.read(s)
  </code></pre>
  
  <p>Unfortunatelly, when trying to use it</p>
  
  <pre><code>def f(s: String): Foo = convert(s)
  </code></pre>
  
  <p>I get compiler errors like</p>
  
  <pre><code>error: ambiguous implicit values:
   both value fooReads of type =&gt; Reads[Foo]
   and value barReads of type =&gt; Reads[Bar]
   match expected type Reads[A]
         def f(s: String): Foo = convert(s)
                                        ^
  </code></pre>
  
  <p>To me code seems simple and right. <code>Reads[Foo]</code> and <code>Reads[Bar]</code> is a completely different types, what is ambiguous about it?</p>
  
  <p>The real code is much more complicated and uses <code>play.api.libs.json</code> but this simplified version is sufficient to reproduce the error.</p>