2015-04-06 127 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]並沒有什麼意義。