【发布时间】:2014-01-13 22:36:48
【问题描述】:
问题
我有两个类如下所示:
class Now {
def do[A](f: Int => A): Seq[A]
}
class Later {
def do[A](f: Int => A): Future[Seq[A]]
}
这两个类的唯一区别是Now返回一个Seq,而Later返回一个Future Seq。我希望这两个类共享同一个接口
我的尝试
考虑到 Seq 和 Future[Seq] 应该只需要一个类型参数,这似乎非常适合更高种类的类型。
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]]
}
我是否错误地使用了更高种类的类型?我是否错误地提供了 Future[Seq]?有没有办法让现在和以后共享同一个界面?
【问题讨论】:
标签: scala generics higher-kinded-types