我有这样的代码:
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]它给出了“协变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"就行了
发布于 2012-08-22 04:05:47
不确定您的情况,但是scalaz-7树(以及M2发行版)有协变型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]https://stackoverflow.com/questions/12065954
复制相似问题