【问题标题】:How to compose monad transformers in scalaz如何在 scalaz 中组成 monad 转换器
【发布时间】:2015-01-29 08:10:00
【问题描述】:

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

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

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

【问题讨论】:

  • 你想在这里建模什么?失败的未来与成功的未来有何不同,左侧为(),右侧为None
  • 这是一个简化的例子。你可以用任何你喜欢的类型代替 Unit 和 Int。

标签: scala scalaz


【解决方案1】:

您只需要显式传递类型(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 转换器所做的相同的事情;虽然它还不是非常成熟)

【讨论】:

  • 我认为OptionT#optionT 只是将OptionT#apply 实现为自然转换,以避免重复包含的类型:optionT[FutureEither](EitherT(ee))
猜你喜欢
  • 1970-01-01
  • 2015-03-12
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多