[swift-evolution] Better syntax for deferred?

Maury Markowitz maury.markowitz at gmail.com
Sat Jan 2 08:25:17 CST 2016


I'm confused about 'defer'. Not the purpose, the chosen syntax. 

func confusing() {
    print("1")
    defer { print("2") }
    print("3")
}

Produces 1,3,2. Trying to describe what is happening here is non-trivial... it runs line 1, then we put line 2 on a stack, then line three runs, then we pop the stack... what?! And stepping through it in the debugger... ugh.

Unless I missed something obvious, wouldn't placing "code that always has to run at the end" actually *at the end* not make more sense? Like this...

func clear() {
    print("1")
    print("3")

    always { print("2") }
}

Not only is the code clearly expressing its intent, the actual execution is following the source.




More information about the swift-evolution mailing list