Sometimes I get really baffled by Swift's compiler.
Why doesn't B compile?
Why can't I put an anonymous closure argument inside a defer block?
struct Example {
var value = 0
mutating func doSomething( _: (inout Self) -> () ) {
/* .... */
}
}
var example = Example()
// A: OK
example.doSomething {
$0.value = 1
/* .... */
}
// B: ERROR!
example.doSomething {
let saveValue = $0.value
defer {
$0.value = saveValue // Anonymous closure argument not contained in a closure
}
$0.value = 1
/* .... */
}
// C: OK, so the issue seems to be just defer
example.doSomething {
do {
$0.value = 1
/* .... */
}
}
// D: OK
example.doSomething { examp in
let saveValue = examp.value
defer {
examp.value = saveValue
}
examp.value = 1
/* .... */
}