2012-08-22 39 views
1

我有這樣的代碼:Writer可以做協變嗎? WriterT [F [_],W,A]可以用+ A來聲明嗎? (解決)

type StringValidation[+A] = Validation[String, A] 
type WriterValidation[A] = WriterT[StringValidation, String, A] 
type Result[A] = WriterValidation[A] 

private def someResult: Result[Int] 
def implementTrait: Result[Any] = someResult // type mismatch 

它給類型不匹配,發現Result[Int],需要Result[Any],但如果將它改變爲:

type WriterValidation[+A] = WriterT[StringValidation, String, A] 

它給出「協變型中出現在WriterT中不變的位置...「

它發生在我身上,操作應該在概念上是好的,Validation可以是協方差,爲什麼WriterT不能(或不是)下降WriterT[F[_], W, +A](或甚至+W)?

我正在使用scalaz7快照,但是我看到6.0.4中的WriterT的聲明是相同的。


事實證明,我使用了錯誤的版本,我使用的是"org.scalaz" %% "scalaz-core" % "7.0-SNAPSHOT",有一次我切換到"org.scalaz" % "scalaz-core_2.9.2" % "7.0.0-M2"它的確定

回答

2

不知道你的情況,但scalaz七樹(以及M2發佈)有 covariant type args

sealed trait WriterT[F[+_], +W, +A] { ... 

而且以下工作:

scala> type SV[+A] = Validation[String, A] 
defined type alias SV 

scala> type WV[+A] = WriterT[SV, String, A] 
defined type alias WV 

scala> type Result[+A] = WV[A] 
defined type alias Result 

scala> def someResult: Result[Int] = ??? 
someResult: Result[Int] 

scala> def implementTrait: Result[Any] = someResult 
implementTrait: Result[Any] 
+1

事實證明,我使用了錯誤的版本,我所用的是' 「org.scalaz」 %% 「scalaz核」 %「7.0-SNAPSHOT 「',一旦我切換到'」org.scalaz「%」scalaz-core_2.9.2「%」7.0.0-M2「'沒關係 – Chris

相關問題