In the following code I have no difficulty in understanding that x is first captured by the outer closure fn and the captured variable is then captured by the inner closure (foo's argument).
func foo(_ fn: () -> Void) {
fn()
}
func test1() {
var x = 0
let fn = { foo { x += 1 } }
}
But if I modify the code a bit by define the inner closure first, I'm not sure what's the right way to understand it. I think x is still captured by the outter closure fn2, right? Or does fn2 only captures fn1?
func test2() {
var x = 0
let fn1 = { x += 1 }
let fn2 = { foo(fn1) }
}
func test3() {
var x = 0
let fn1 = { x += 1 }
let fn2 = { fn1() }
}
I ask this because I'm trying to understand various forms in which a sending closure re-sends a sending value. I intentionally keep the above examples simple and have no sending closure invovled.