Search code examples
scalatypesaliasscala-catseither

EitherT with Scala type alias


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!


Solution

  • Just type parameters were not inferred. Try

    for {
      a <- EitherT[Future, Error, Int](fA(7))
      b <- EitherT[Future, Error, Int](fB(42))
    }