简体   繁体   中英

Scala type class to extend generic type: No implicits found for parameter

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.

Imagine you have a generic type MyList[A] :

trait MyList[A]

object MyList {
  case class Empty[A]() extends MyList[A]
  case class Node[A](value: A, next: MyList[A]) extends MyList[A]
}

Now you want to add some behavior to this class, eg to convert it into a Stream[A] . A type class based extension would seem appropriate:

// inspired by https://scalac.io/typeclasses-in-scala

trait MyListExt[T, A <: MyList[T]] {
  def stream(a: A): Stream[T]
}

object MyListExt {
  def apply[T, A <: MyList[T]](implicit a: MyListExt[T, A]): MyListExt[T, A] = a

  object ops {
    implicit class MyListExtOps[T, A <: 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() => acc
        case MyList.Node(value, next) => fold(next, acc :+ value)
      }
      fold(a, Stream.empty)
    }
  }
}

When I now try to use this type class in my code, I get the following error at l.stream :

 No implicits found for parameter ext: MyListExt[T_, MyList[Int]] 
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)
  }
}

What am I doing wrong, or how can I get my l.stream to work?
I have seen many examples involving type classes and implicit conversion, but none so far operating on generic types.

  implicit def myListToStreamImpl[T]: MyListExt[T, MyList[T]] = new MyListExt[T, MyList[T]] {
    override def stream(a: MyList[T]): Stream[T] = {
      def fold(l: MyList[T1], acc: Stream[T1]): Stream[T1] = l match {
        case MyList.Empty() => acc
        case MyList.Node(value, next) => fold(next, acc :+ value)
      }
      fold(a, Stream.empty[T1])
    }
  }

Your types don't align because you've used that private type for whatever bizarre reason. Types nested inside objects have a completely different application, and they don't relate to your current use case.

The trouble is that in l.stream.foreach(println) the l is implicitly transformed to new MyListExt.ops.MyListExtOps[.., ..](l) and generics are inferred to be [Nothing, MyList[Int]] , which doesn't satisfy [T, A <: MyList[T]] .

I can't see reason to parametrize MyListExt with both T and A <: MyList[T] . I guess T is enough, use MyList[T] instead of A .

Don't use private type T0 , just parametrize myListToStreamImpl (make it def ) with T0 aka T .

Try

  trait MyList[A]

  object MyList {
    case class Empty[A]() extends MyList[A]
    case class Node[A](value: A, next: MyList[A]) extends MyList[A]
  }

  trait MyListExt[T] {
    def stream(a: MyList[T]): Stream[T]
  }

  object MyListExt {
    def apply[T](implicit a: MyListExt[T]): MyListExt[T] = a

    object ops {
      implicit class MyListExtOps[T](val a: MyList[T]) extends AnyVal {
        def stream(implicit ext: MyListExt[T]): Stream[T] = ext.stream(a)
      }
    }

    implicit def myListToStreamImpl[T]: MyListExt[T] = new MyListExt[T] {
      override def stream(a: MyList[T]): Stream[T] = {
        def fold[T1](l: MyList[T1], acc: Stream[T1]): Stream[T1] = l match {
          case MyList.Empty() => acc
          case MyList.Node(value, next) => fold(next, acc :+ value)
        }
        fold(a, Stream.empty)
      }
    }
  }

  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)
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM