scala期货和回退
我在下一页阅读有关期货和回退的使用: 我没有看到我期待的行为 我有以下测试(摘自页面并稍作修改): 我一直在尝试在quote futures中注释/取消注释sys.error()调用。如果没有系统错误,则输出“Value:20$”。启用第一个sys.error()会给我“Value:15CHF”。到目前为止还不错,但是当我启用第二个sys.error()时,据我所知,它应该会给我第一个未来的例外,即“error…1”,但它会给我“error…2” 有人能解释一下我做错了什么吗 谢谢,scala期货和回退,scala,Scala,我在下一页阅读有关期货和回退的使用: 我没有看到我期待的行为 我有以下测试(摘自页面并稍作修改): 我一直在尝试在quote futures中注释/取消注释sys.error()调用。如果没有系统错误,则输出“Value:20$”。启用第一个sys.error()会给我“Value:15CHF”。到目前为止还不错,但是当我启用第二个sys.error()时,据我所知,它应该会给我第一个未来的例外,即“error…1”,但它会给我“error…2” 有人能解释一下我做错了什么吗 谢谢, Marc
Marc.文档似乎与实现不一致 我从scala v2.10.3中获取了这些代码片段
// In Future.scala
def fallbackTo[U](that: Future[U]): Future[U] = {
val p = Promise[U]()
onComplete {
case s @ Success(_) => p complete s // first future succeeded
case _ => p completeWith that // first future failed, complete promise with second
}
p.future
}
// In Promise.scala
final def completeWith(other: Future[T]): this.type = {
other onComplete { this complete _ } // complete the promise with second future's result
this
}
我们可以看到,无论是第一个未来的成功,还是第二个未来的成功/失败,都将实现这一承诺
因此,如果两个期货都失败,我们将获得第二个期货的失败。这似乎是实现中的一个错误,请参阅此问题:
// In Future.scala
def fallbackTo[U](that: Future[U]): Future[U] = {
val p = Promise[U]()
onComplete {
case s @ Success(_) => p complete s // first future succeeded
case _ => p completeWith that // first future failed, complete promise with second
}
p.future
}
// In Promise.scala
final def completeWith(other: Future[T]): this.type = {
other onComplete { this complete _ } // complete the promise with second future's result
this
}