3
爲什麼這Scala代碼編譯, 恢復的簽名,然後從異常中恢復,斯卡拉未來
def recover[U >: T](pf: PartialFunction[Throwable, U])(implicit executor: ExecutionContext): Future[U]
爲什麼下面的代碼編譯。您可以在下面的代碼中看到recover
未返回單元。
object TestRecover {
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
def failingFunction(input: Seq[String]): Future[Unit] = {
Future {
if (input.isEmpty) throw new Exception("Cannot be empty")
else()
}
}
def callFailingFunc(input: Seq[String]): Future[Unit] = {
failingFunction(input).recover {
case _ =>
//Not returning Unit here, but Future[Unit]. Shouldn't type checker fail this ?
callFailingFunc(input.reverse)
}
}
}
此外爲什麼Await.result(TestRecover.callFailingFunc(Seq()), 20.seconds)
由於無限遞歸不產生計算器?