I am using monix for side effects and ended with this type
Task[Either[A, Task[B]]]
, Is there a way to get Task[Either[A, B]]
?
So far all I could do is convert Task[Either[A, Task[B]]]
to Task[Any]
, basically removing Either
using pattern matching and flattening, but missing type information in process
val tEitherT: Task[Either[A, Task[B]]] = ???
val finalType: Task[Any] =
tEitherT.map(either => {
either match {
case Right(value) => value // Task[B]
case Left(value) => Task(value) // Lift
}
}).flatten
import monix.eval.Task
import cats.implicits._
import cats.Traverse
trait A
trait B
def tEitherT: Task[Either[A, Task[B]]] = ???
type EitherRight[T] = Either[A,T] // or use kind-projector plugin instead
val finalType: Task[Either[A, B]] =
tEitherT.flatMap(Traverse[EitherRight].sequence(_))
In scala 2.13 or with option -Ypartial-unification
you can simplify to tEitherT.flatMap(_.sequence)
. See second answer here.