In real life code, I want to have a defer issued "up top" of a method, but in one edge case I'd like to have another defer that runs before the original one (now thinking about this I may be able to solve it in a traditional sense).
I saw something online that suggested using 'defer { defer { <code> } }' In fact that does work, but generates a warning in Xcode 11b4-beta, with a fix suggestion of using 'defer { do { <code> } }'. That actually works - but I don't understand why!
var goo = 0
while goo < 10 {
defer { print("LAST", goo) }
defer { defer { print("(Want First)", goo) } } // want it to execute before the LAST print but clang warning
defer { do { print("First") } } // what the compiler tells me to do
goo += 1
}
Console output:
First
(Want First) 1
LAST 1
So the double defer does do what I want, which I understand, and the 'defer-do' also works, but I'd l;ove to know why it works!