Creating instances of a covariant type class from instances of a non-covariant one <p>Suppose I've got a simple type class whose instances will give me a value of some type:</p> <pre><code>trait GiveMeJustA[X] { def apply(): X } </code></pre> <p>And I've got some instances:</p> <pre><code>case class Foo(s: String) case class Bar(i: Int) implicit object GiveMeJustAFoo extends GiveMeJustA[Foo] { def apply() = Foo("foo") } implicit object GiveMeJustABar extends GiveMeJustA[Bar] { def apply() = Bar(13) } </code></pre> <p>Now I have a similar (but unrelated) type class that does the same thing but is covariant in its type parameter:</p> <pre><code>trait GiveMeA[+X] { def apply(): X } </code></pre> <p>In its companion object we tell the compiler how to create instances from instances of our non-covariant type class:</p> <pre><code>object GiveMeA { implicit def fromGiveMeJustA[X](implicit giveMe: GiveMeJustA[X]): GiveMeA[X] = new GiveMeA[X] { def apply() = giveMe() } } </code></pre> <p>Now I'd expect <code>implicitly[GiveMeA[Foo]]</code> to compile just fine, since there's only one way to get a <code>GiveMeA[Foo]</code> given the pieces we have here. But it doesn't (at least not on either 2.10.4 or 2.11.2):</p> <pre><code>scala&gt; implicitly[GiveMeA[Foo]] &lt;console&gt;:16: this.GiveMeA.fromGiveMeJustA is not a valid implicit value for GiveMeA[Foo] because: hasMatchingSymbol reported error: ambiguous implicit values: both object GiveMeJustAFoo of type GiveMeJustAFoo.type and object GiveMeJustABar of type GiveMeJustABar.type match expected type GiveMeJustA[X] implicitly[GiveMeA[Foo]] ^ &lt;console&gt;:16: error: could not find implicit value for parameter e: GiveMeA[Foo] implicitly[GiveMeA[Foo]] ^ </code></pre> <p>If we get rid of our irrelevant <code>GiveMeJustA</code> instance, it works:</p> <pre><code>scala&gt; implicit def GiveMeJustABar: List[Long] = ??? GiveMeJustABar: List[Long] scala&gt; implicitly[GiveMeA[Foo]] res1: GiveMeA[Foo] = GiveMeA$$anon$1@2a4f2dcc </code></pre> <p>This is in spite of the fact that there's no way we can apply <code>GiveMeA.fromGiveMeJustA</code> to this instance to get a <code>GiveMeA[Foo]</code> (or any subtype of <code>GiveMeA[Foo]</code>).</p> <p>This looks like a bug to me, but it's possible that I'm missing something. Does this make any sense? Is there a reasonable workaround?</p>