【问题标题】:Lifting A Future using EitherT.liftF When Value Is Already a Future当价值已经是未来时,使用 EitherT.liftF 提升未来
【发布时间】:2020-02-18 16:24:34
【问题描述】:

我有这样的价值:

val ss: Option[Future[List[Either[Error, File]]]]

而我想做的就是把它提升到EitherT.liftF[Future, Error, List[Either[Error, File]]],所以我做的是这样的:

    ss match {
      case Some(value) => EitherT.liftF[Future, Error, List[Either[Error, File]]](value)
      case None        => EitherT.leftT[Future, List[Either[Error, File]]](Error("failed"))
    }

我的问题是我可以使用 EitherT.liftF 来提升一个已经是未来的值是否正确,因为我认为通常它用于需要提升到未来的值,而不是未来的值自己。

【问题讨论】:

  • 是的,没错,就是使用EitherT.liftF - 查看文档:typelevel.org/cats/api/cats/data/…
  • 好吧,未来一开始甚至都无法取消……但说实话。不知道将 FutureEitherT 一起使用是否有问题,我从未在实际代码中使用过期货。

标签: scala scala-cats


【解决方案1】:

Future 有一个 Monad 实例出于实际原因,但实际上并不合法。有关该主题,请参阅此 post

但是,在许多情况下,例如您的情况,它仍会按预期运行。

你也可以这样实现:

EitherT(ss.toRight(Error("failed")).sequence)

【讨论】:

    猜你喜欢
    • 2017-10-13
    • 2016-07-29
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    相关资源
    最近更新 更多