Try to use type alias with EitherT get compilation error
type FuEiErr[T] = Future[Either[Error, T]]
type FuEiErrInt = Future[Either[Error, Int]]
case class Error(msg: String)
def fA(x:Int): FuEiErr[Int] = Future(Right(x)) // compile error
def fB(x:Int): FuEiErr[Int] = Future(Right(x))
// def fA(x:Int): FuEiErrInt = Future(Right(x)) // ok
// def fB(x:Int): FuEiErrInt = Future(Right(x))
@main def TestEitherT(): Unit =
(for {
a <- EitherT(fA(7))
b <- EitherT(fB(42))
} yield { (b) }).value.map {
case Left(err) => println(s"Error: ${err.msg}")
case Right(res) => println(s"Result: ${res}")
}
Got error "No given instance of type cats.Functor[F] was found for parameter F of method map in class EitherT where: F is a type variable with constraint >: [X0] =>> scala.concurrent.Future[Either[Error, X0]] scala.concurrent.Future[X0] and <: [_] =>> Any
Thanks for help!
EitherTis usually a bad idea and using cats withFutureis a bad idea as well.EitherTdoesn't play well with concurrency. cats makes zero guarantees aboutFutureexecution order, especially withtraverseand especially when combined withEitherT. Is all just a mix for a disaster. IME is way simpler to just stick to concreteIOand use the failure channel or custom ADTs.Futurecool, do what you prefer. But don't mix it with cats, it will cause you trouble. Unless you don't care at all about the execution behaviour of your operations, at that point then don't useFutureat all in any case.