我有一个类似的方法:
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 的新手:/
答案 0 :(得分:3)
正如评论部分所建议的那样,用于理解的类型应该对齐,编译器会抱怨:您在 flatMap
上调用 EitherT
(通过 <-
隐式调用)并且作为结果返回 Future
而不是预期的 EitherT
。
您需要更改返回类型并重新实现下一个方法:
private def _createConfig(app: String): EitherT[Future, Config, Throwable] = EitherT {
secret.map { secret =>
Right(Config(action = s"SP_$app", clientSecret = secret)
}
}