In Capturing Values section of swift documentation:
... It does this by capturing a reference to runningTotal and amount from the surrounding function and using them within its own function body. ...
and
If you create a second incrementer, it will have its own stored reference to a new, separate runningTotal variable.
So I try the following testing and get a conclusion that since each incrementer made by the makeIncrementer(forIncrementer:)
has its own runningTotal
and the value of runningTotal
is remembered by each incrementer, what values-capturing does is equivalent to creating some static variable(s) for a closure. But I'm not sure whether my statement is correct.
func makeIncrementer(forIncrement amount: Int) -> () -> Int {
var runningTotal = 0
func incrementer() -> Int {
runningTotal += amount
return runningTotal
}
return incrementer
}
let by10Incrementer = makeIncrementer(forIncrement: 10)
let by20Incrementer = makeIncrementer(forIncrement: 20)
for i in 0..<5 {
print(by10Incrementer())
}
print("---")
for j in 0..<5 {
print(by20Incrementer())
}
output:
10
20
30
40
50
---
20
40
60
80
100
I originally thought that the output of by20Incrementer()
would start from 50, but it starts from 0. So the runningTotal
of each incrementer refers to different one.