Search code examples
scalafor-comprehensioneither

EitherT in Scala not working with For Comprehension


I have this code:

 (for {
      oldResult <- EitherT[Future, A, B](getById(id))
      newResult <- EitherT.right(update(changeOldData(oldResult)))
    } yield newResult).value

Where the functions returns

 getById       -> Future[Either[A, B]]
 update        -> Future[B]
 changeOldData -> B

The entire block is suppose to return:

Future[Either[A, B]]

In IntelliJ, there is no complains about the code above, but when compiling, I'm getting the following error:

[error]  found   : B => cats.data.EitherT[scala.concurrent.Future,Nothing,B]
[error]  required: B => cats.data.EitherT[scala.concurrent.Future,A,B]
[error]           oldResult <- EitherT[Future, A, B](

I tried to not include the type and I'm getting the same error as well. Any idea why?


Solution

  • When you call EitherT.right(..) the compiler cannot figure out what the left type of your either should be. That's why the error message says it found Nothing instead of A. You need to help it out a bit.

    EitherT.right[A](update(changeOldData(oldResult)))
    

    This will compile.