[swift-users] Details of defer statement in Swift
clattner at apple.com
Thu Nov 10 15:13:25 CST 2016
> On Nov 9, 2016, at 11:45 PM, Rien <Rien at balancingrock.nl> wrote:
> The manual says this:
> https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html <https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html>
> A defer statement is used for executing code just before transferring program control outside of the scope that the defer statement appears in.
> The scope of a for loop ends when the loop ends.
Right, more specifically, it is the “static” scope that it is defined in. Go has a similar but different defer statement, which runs defer'd actions at the end of the current dynamic *function* scope. The Swift rules are simpler, more predictable, and are able to be implemented more efficiently.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-users