考虑以下代码片段:
class MyClass {
var a = _
var b = _
...
var y = _
val z = (for {
a0 <- someMethodReturningFuture
b0 <- someMethodReturningFuture(a0)
...
z0 <- someMethodReturningFuture(y0)
} yield {
a = a0
b = b0
...
y = y0
someCalculation(y)
}).result
}
是否可以执行这样的初始化但使用
val
s 而不是 var
s(以某种方式)?关键是,我不想阻塞并等待每个中间结果,只是为了最后一个。 最佳答案
除非你有超过 22 个变量:
val (a, b, c, d, ..., v) = (for {
a0 <- someMethodReturningFuture
b0 <- someMethodReturningFuture(a0)
...
u0 <- someMethodReturningFuture(t0)
} yield {
(a0, b0, ..., u0, someCalculation(u0))
}).result
关于scala - 从 future 链初始化的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11500620/