1

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
Bogdan Vakulenko
  • 3,380
  • 1
  • 10
  • 25
Satya
  • 13
  • 1
  • 2

2 Answers2

4

Try this:

tEitherT.flatMap {
  case Right(taskB) => taskB.map(Right(_))
  case Left(a) => Task(Left(a))
}

Try it out!

Markus Appel
  • 3,138
  • 1
  • 17
  • 46
lprakashv
  • 1,121
  • 10
  • 19
2
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.

Bogdan Vakulenko
  • 3,380
  • 1
  • 10
  • 25