【发布时间】:2021-06-27 03:36:09
【问题描述】:
我有一个类似的方法:
private def _createConfig(
app: String,
): Future[Config] = {
secret.map { secret =>
Config(
action = s"SP_$app",
clientSecret = secret,
)
}
}
还有另一个消耗这个的方法:
private def setup(
app: String,
someCondition: Boolean = false
): EitherT[Future, Throwable, String] = {
Given("I create a partner")
for {
something <- createSomething()
_ = if (someCondition) And(s"I do some action") else Done
_ <- if (someCondition) createAnotherSomething(something) else doneEitherT
_ = Then(s"I create a configuration for partner $partner and app $app")
_ <- _createConfig(app)
} yield something
}
doneEitherT 定义为:
final lazy val doneEitherT = Future.successful(Done.upcast).rightT[Throwable]
当我构建它时,编译器会抱怨类型不匹配:
type mismatch;
found : scala.concurrent.Future[String]
required: cats.data.EitherT[scala.concurrent.Future,Throwable,String]
_ <- _createConfig(app)
为什么编译器期望_createConfig(app) 是EitherT 类型? for 块中的所有方法都应该与方法的签名匹配吗? Scala 新手并使用 Futures :/
【问题讨论】:
-
根据经验,for-comprehension 中
<-的每个右侧都必须属于同一类型,因此在您的情况下为EitherT[Future, Throwable, A]。 -
你能发个链接吗?即文档 - 我想更好地理解这一点
-
这是一个经验法则。如果您想获得更好的理解,请查看如何对理解进行脱糖,然后查看在
EitherT上如何定义flatMap。