我正在学习 Scala 中的 monad 转换器,但我遇到了一个我认为目前无法解决的问题。在我的 monad 转换器堆栈中,我组成了 Either 和 State monad。但是,我没有调用属于两个单子之一的函数:
import scalaz._
import Scalaz._
object Minimal {
type Inner[A] = EitherT[Id, String, A]
type Outer[F[+_], A] = StateT[F,Int,A]
type Stack[A] = Outer[Inner, A]
def foo:Stack[Int] = for {
n <- get[Int]
} yield {
2 * n
}
def main(args: Array[String]): Unit = {
val x = foo.eval(8)
println(x)
}
}
失败并显示以下错误消息:
[error] Minimal.scala:10: type mismatch;
[error] found : scalaz.IndexedStateT[scalaz.Id.Id,Int,Int,Int]
[error] required: Minimal.Stack[Int]
[error] (which expands to) scalaz.IndexedStateT[Minimal.Inner,Int,Int,Int]
[error] n <- get[Int]
如果我将 monad 变压器堆栈更改为:
type Stack[A] = State[Int,A]
该程序编译并运行没有问题。有人知道我在这里做错了什么吗?