如何在 scalaz 中编写 monad 转换器

How to compose monad transformers in scalaz

如何进行编译,或做类似的事情?

import scala.concurrent.Future
import scalaz._
import Scalaz._

val ee: Future[Unit \/ Option[Int]] = Future(\/-(Option(1)))
OptionT.optionT(EitherT.eitherT(ee))

您只需要显式传递类型(OptionT.optionT 似乎做了一些稍微不同的事情):

//could use a type lambda, but this is (IMO) slightly clearer
type FutureEither[A] = EitherT[Future, Unit, A]
new OptionT[FutureEither, Int](EitherT.eitherT[Future, Unit, Option[Int]](ee))

(我可能应该提到我的 scalaz-transfigure 库作为一种更轻量级和更推理的方法来做与 monad 转换器相同的事情;虽然它还不是很成熟)