2015-04-06 27 views
0

鉴于以下trait(从这个有用shapeless talk):特质与更高Kinded类型

scala> trait NatT[F[_], G[_]] { def apply[T](f: F[T]): G[T] } 
warning: there were two feature warnings; re-run with -feature for details 
defined trait NatT 

我认为,这意味着NatT接受两个高kinded参数:FG

在这个假设下,我试图让一个实例,其中F和G Option类型:

scala> case object Maybe extends NatT[Option, Option] { 
    | override def apply(f: Option[Int]) = f 
    | } 
<console>:8: error: object creation impossible, since method apply in trait NatT of type [T](f: Option[T])Option[T] is not de 
fined 
     case object Maybe extends NatT[Option, Option] { 
       ^
<console>:9: error: method apply overrides nothing. 
Note: the super classes of object Maybe contain the following, non final members named apply: 
def apply[T](f: Option[T]): Option[T] 
     override def apply(f: Option[Int]) = f 
        ^

我怎样才能解决这个企图在作出Maybe实例?

回答

6

您的apply方法缺少类型参数。就那么简单。

case object Maybe extends NatT[Option, Option] { 
    def apply[A](f: Option[A]): Option[A] = f 
} 

你尝试定义apply没有一个类型参数被看作是一种不同的方法,所以看来apply未实现。鉴于FG应该是更高的类型,试图将它们修复为Option[Int]并没有什么意义。