如何修复:"found : (AnyVal, AnyVal) required: (Double, Double)" 与 Futures

How to fix: "found : (AnyVal, AnyVal) required: (Double, Double)" with Futures

编译错误:

[error] found : ((Double, Double)) => scala.concurrent.Future[(Double, Double)]

[error] required: ((AnyVal, AnyVal)) => scala.concurrent.Future[?] [error](c: (Double, Double)) => Future(c).map(x=>x)

IDE 正在接受代码。

代码是使用 for 循环和 yield 编写的。我试图简化它。

def f(r: UUID, l: Int) = {
  g(r).flatMap { (c: (Double, Double)) => /*** this part is just for the debugging ***/
    Future(c).map(x=>x)
  }
}

def g(r: UUID) = {
  session.selectOne(
    s"""
      SELECT
        ${RR.x},
        ${RR.y}
      FROM
        ${RR.g}
      WHERE
        ${RR.r} = ?
      ORDER BY
        ${RR.t} DESC
    """,
    r.toString
  ).map {
    case Some(row) => (1.0,1.0) //will be replaced when it is working
    case None => (0,0)
  }
}

编译器发现 c 的类型是 (AnyVal, AnyVal) 我希望它是 (Double ,双)

它实际上是在抱怨你传递给 flatmap 的方法的函数签名不是它所需要的。尝试在 g 函数中将 case None => (0,0) 行更改为 case None => (0d, 0d)。如果两个 case 分支返回不同的类型,它将默认为两者的超类型。在这种情况下,您的 g 函数似乎正在返回类型 (AnyVal,AnyVal)