[英]Lift \/[A, B] into EitherT[Future, A, B]
如何使用point/liftM
语法将\\/[Error, Int]
提升为EitherT[Future, Error, Int]
,以便提升在右侧?
我有以下场景
for {
r1 <- f1: EitherT[Future, Error, Int]
r2 <- v: \/[Error, Int]
r3 <- f2: EitherT[Future, Error, Int]
} yield r3
我可以让v
通过应用适合EitherT.fromDisjunction[Future]
像这样
for {
r1 <- f1
r2 <- EitherT.fromDisjunction[Future](v)
r3 <- f2
} yield r3
或者干脆
for {
r1 <- f1
r2 <- EitherT(Future(v))
r3 <- f2
} yield r3
但是我试图将提升魔法移动到v
的右侧,就像这样
for {
r1 <- f1
r2 <- v.point[Future].liftM[EitherT] // something approximately like this
r3 <- f2
} yield r3
我试过了
type Result[F[_], A] = EitherT[F, Error, A]
v.point[Future].liftM[Result]
和
v.point[({ type L[x] = EitherT[Future, Error, x] })#L]
EitherT[Future, Error, Error \/ Int]
虽然我需要
EitherT[Future, Error, Int]
将提升移到右侧只是为了美观,因为EitherT.fromDisjunction[Future]
工作得很好。
只是
r2 <- v.eitherT[Future, Error, Int]
同
import scalaz.{EitherT, \/}
import scalaz.std.scalaFuture._
import scalaz.syntax.eithert._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.language.higherKinds
我不确定scalaz中是否存在这种情况。 toEitherT[F]
没有任何toEitherT[F]
。 但是添加新语法是微不足道的:
implicit class EitherLiftSyntax[A, B](val self: A \/ B) extends AnyVal {
def liftT[M[_]]: EitherT[M, A, B] = EitherT.fromDisjunction[M](self)
}
只要你有这个范围,你可以说v.liftT[Future]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.