Scala 中的 T 要么不与 For Comprehension 合作



我有这个代码:

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

函数返回的位置

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

整个块应该返回:

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(..)时,编译器无法弄清楚你的左类型应该是什么。这就是为什么错误消息说它找到了Nothing而不是A。你需要帮忙一点。

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

这将编译。

最新更新