斯卡拉<- for loop flatMap annotation - defining type

问题描述 投票:0回答:1

我在 scala 中使用 for 循环进行这样的 flatMap 注释

for {
  conf <- configRepository.getConf(key)
  info <- customRepository.getInfo(id)
  _ <- DBIO.from(getAdditionalInfo(info))
} yield ()

上线了_<- DBIO.from(getAdditionalInfo(info)) I am getting this info

[error]  found   : Seq[Either[CustomService.Error,Unit]] => slick.dbio.DBIOAction[Unit,slick.dbio.NoStream,Nothing]
[error]  required: Seq[Either[CustomService.Error,Unit]] => slick.dbio.DBIOAction[Unit,slick.dbio.NoStream,E2]

有什么办法,如何定义<- (flatMap) ? Or I will need to rewrite for loop to flatMap and define type like this

flatMap[Seq[Either[CustomService.Error, Unit], NoStream, Effect.All]]

的返回类型
scala
1个回答
0
投票

根据错误消息,无法推断类型参数

E

https://scala-slick.org/doc/stable/api/slick/dbio/DBIOAction.html

sealed trait DBIOAction[+R, +S <: NoStream, -E <: Effect]

如何定义 <- (flatMap) ?

的返回类型

您可以尝试添加类型归属:

for {
  a <- fa
  b <- fb
  c <- (fc : Type_of_fc)
} yield d

for {
  a <- fa
  b <- fb
  (c: Type_of_c) <- fc
} yield d

这要么有助于类型推断,要么您将遇到新的编译错误,并看到您尝试在需要不同类型的情况下替换某种类型的值。

© www.soinside.com 2019 - 2024. All rights reserved.