Scala: Generics For Return Type Seq[A] or Future[Seq[A]]
generics, higher-kinded-types, scala
Solution
You need type composition:
trait Composition[F[_], G[_]] { type T[A] = F[G[A]] }
class Later extends Do[Composition[Future, Seq]#T] {
def do[A](f: Int => A): Future[Seq[A]]
}
Or if you just need it in this one place
class Later extends Do[({ type T[A] = Future[Seq[A]] })#T] {
def do[A](f: Int => A): Future[Seq[A]]
}
See scalaz (I could have sworn it included general type composition, but apparently not.)
Problem
The Problem I have two classes that look like the following: ``` class Now { def do[A](f: Int => A): Seq[A] } class Later { def do[A](f: Int => A): Future[Seq[A]] } ``` The only difference between the two classes is that Now returns a Seq and Later returns a Future Seq. I would like these two classes to share the same interface What I Have Tried This seemed like a perfect fit for higher-kinded types, considering how both Seq and Future[Seq] should only need one type parameter. ``` trait Do[F[_]] { def do[A](f: Int => A): F[A] } // Compiles class Now extends Do[Seq] { def do[A](f: Int => A): Seq[A] } // Does not compile. "type Seq takes type parameters" and // "scala.concurrent.Future[<error>] takes no type parameters, expected: one" class Later extends Do[Future[Seq]] { def do[A](f: Int => A): Future[Seq[A]] } ``` Am I using higher-kinded types incorrectly? Am I supplying Future[Seq] incorrectly? Is there a way to allow Now and Later to share the same interface?