【问题标题】:EitherT in Scala not working with For ComprehensionScala 中的 EitherT 不能与 For Comprehension 一起使用
【发布时间】:2020-01-29 12:57:49
【问题描述】:

我有这个代码:

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

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

【问题讨论】:

  • 重要的是要注意 IntelliJ 的 Scala 演示编译器没有完整的功能。所以永远不要依赖 int。

标签: scala for-comprehension either


【解决方案1】:

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

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

这将编译。

【讨论】:

  • 谢谢,这解决了问题。有趣的是,错误与 For 的第一部分有关,而不是第二部分。
猜你喜欢
  • 2014-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2015-12-30
  • 2013-12-25
  • 1970-01-01
相关资源
最近更新 更多