Scala:未来,拉链和恢复

时间:2014-07-05 17:38:00

标签: scala future

以下代码按预期编译并运行...

def find1(selector: JsValue): Future[Seq[String]]
def find2(selector: JsValue): Future[Seq[String]]

find1(Json.obj("name" -> "Joe")) zip
find2(Json.obj("name" -> "Tim")) map { case (result1, result2) =>
  val result = result1 ++ result2
  ...
}

...但如果我添加recover来处理可能的错误......

find1(Json.obj("name" -> "Joe")) zip
find2(Json.obj("name" -> "Tim")) map { case (result1, result2) =>
  val result = result1 ++ result2
  ...
}.recover { case e =>
  ...
}

...我总是收到以下错误:

[error] /home/j3d/test/TestController.scala:558: missing parameter type for expanded function
[error] The argument types of an anonymous function must be fully known. (SLS 8.5)
[error] Expected type was: ?
[error]        find2(Json.obj("name" -> "Tim")) map { case (result1, result2) =>
[error]                                             ^
[error] one error found

我试图像这样指定result1result2的类型...

find1(Json.obj("name" -> "Joe")) zip
find2(Json.obj("name" -> "Tim")) map { case (result1: Seq[String], result2: Seq[String]) =>
  val result = result1 ++ result2
  ...
}.recover { case e =>
  ...
}

...但没有任何变化,即它只在没有recover的情况下进行编译。我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

始终使用运算符表示法。

find1(Json.obj("name" -> "Joe")) zip
find2(Json.obj("name" -> "Tim")) map { case (result1, result2) =>
   val result = result1 ++ result2
   ...
} recover { case e => // removed the '.' 
  ...
}