Scala 中的 EitherT 无法使用 For Comprehension

EitherT in Scala not working with For Comprehension

我有这个代码:

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

其中函数 returns

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

整个区块假设为 return:

Future[Either[A, B]]

在 IntelliJ 中,上面的代码没有任何问题,但是在编译时,出现以下错误:

[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](

我试图不包含类型,但我也遇到了同样的错误。 知道为什么吗?

当您调用 EitherT.right(..) 时,编译器无法确定您的 either 的左侧类型应该是什么。这就是错误消息说它找到了 Nothing 而不是 A 的原因。您需要稍微帮助一下。

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

这将编译。