title: Scala type class to extend generic type: No implicits found for parameter
body: <p>I want to write a type class, to add some behavior to a generic type. However, I cannot figure out how to do it; I keep running into the error below.</p>
  
  <p>Imagine you have a generic type <code>MyList[A]</code>:</p>
  
  <pre class="lang-scala prettyprint-override"><code>trait MyList[A]
  
  object MyList {
    case class Empty[A]() extends MyList[A]
    case class Node[A](value: A, next: MyList[A]) extends MyList[A]
  }
  </code></pre>
  
  <p>Now you want to add some behavior to this class, e.g. to convert it into a <code>Stream[A]</code>. A type class based extension would seem appropriate:</p>
  
  <pre class="lang-scala prettyprint-override"><code>// inspired by https://scalac.io/typeclasses-in-scala
  
  trait MyListExt[T, A &lt;: MyList[T]] {
    def stream(a: A): Stream[T]
  }
  
  object MyListExt {
    def apply[T, A &lt;: MyList[T]](implicit a: MyListExt[T, A]): MyListExt[T, A] = a
  
    object ops {
      implicit class MyListExtOps[T, A &lt;: MyList[T]](val a: A) extends AnyVal {
        def stream(implicit ext: MyListExt[T, A]): Stream[T] = ext.stream(a)
      }
    }
  
    private type T0
  
    implicit val myListToStreamImpl: MyListExt[T0, MyList[T0]] = new MyListExt[T0, MyList[T0]] {
      override def stream(a: MyList[T0]): Stream[T0] = {
        def fold[T1](l: MyList[T1], acc: Stream[T1]): Stream[T1] = l match {
          case MyList.Empty() =&gt; acc
          case MyList.Node(value, next) =&gt; fold(next, acc :+ value)
        }
        fold(a, Stream.empty)
      }
    }
  }
  </code></pre>
  
  <p>When I now try to use this type class in my code, I get the following error at <code>l.stream</code>:</p>
  
  <blockquote>
  <pre><code>No implicits found for parameter ext: MyListExt[T_, MyList[Int]]
  </code></pre>
  </blockquote>
  
  <pre class="lang-scala prettyprint-override"><code>object MyListTest {
    def main(args: Array[String]): Unit = {
      import MyListExt.ops._
      val l: MyList[Int] = MyList.Node(1, MyList.Node(2, MyList.Node(3, MyList.Empty())))
      l.stream.foreach(println)
    }
  }
  </code></pre>
  
  <p>What am I doing wrong, or how can I get my <code>l.stream</code> to work?<br>
  I have seen many examples involving type classes and implicit conversion, but none so far operating on generic types.</p>