简体   繁体   中英

Lift \/[A, B] into EitherT[Future, A, B]

How to lift \\/[Error, Int] into EitherT[Future, Error, Int] using point/liftM syntax such that lifting is on the right-hand side?

I have the following scenario

for {
  r1 <- f1: EitherT[Future, Error, Int]
  r2 <- v: \/[Error, Int]
  r3 <- f2: EitherT[Future, Error, Int]
} yield r3

I can make v fit by applying EitherT.fromDisjunction[Future] like so

for {
  r1 <- f1
  r2 <- EitherT.fromDisjunction[Future](v)
  r3 <- f2
} yield r3

or simply

for {
  r1 <- f1
  r2 <- EitherT(Future(v))
  r3 <- f2
} yield r3

however I am trying to move the lifting magic to the right-hand-side of v like so

for {
  r1 <- f1
  r2 <- v.point[Future].liftM[EitherT] // something approximately like this
  r3 <- f2
} yield r3

I tried

type Result[F[_], A] = EitherT[F, Error, A]
v.point[Future].liftM[Result]

and

v.point[({ type L[x] = EitherT[Future, Error, x] })#L]

suggested here and here , however this types to

EitherT[Future, Error, Error \/ Int]

whilst I require

EitherT[Future, Error, Int]

Moving lifting to the right-hand-side is just for aesthetics, as EitherT.fromDisjunction[Future] works fine.

It's just

r2 <- v.eitherT[Future, Error, Int]

with

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

I am not sure if that exists in scalaz. There is no toEitherT[F] on disjunction. However it is trivial to add new syntax:

  implicit class EitherLiftSyntax[A, B](val self: A \/ B) extends AnyVal {
    def liftT[M[_]]: EitherT[M, A, B] = EitherT.fromDisjunction[M](self)
  }

As long as you have that in scope, you can say v.liftT[Future]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM