[swift-evolution] [Proposal] A more liberal placement of defer

Erica Sadun erica at ericasadun.com
Mon Jun 6 16:45:58 CDT 2016


Not sure I see how they're in different scopes. Can you explain that to me?

-- E

> On Jun 6, 2016, at 2:18 PM, donny wals <donnywals at gmail.com> wrote:
> 
> Erica,
> 
> Maybe my phrasing was a bit off, but in my proposal it’s really important that the return and the defer are in the same scope. In your example the allocate memory line is in a different scope that the guard/else statements. Therefor, the defer { release memory } shouldn’t be executed if the guards don’t hold.
> 
> D
> 
>> On 06 Jun 2016, at 22:09, Erica Sadun <erica at ericasadun.com> wrote:
>> 
>> This is problematic. You may want to defer code at different points with different reasons. For example, you might not want to trigger defer until after some preconditions have been met.:
>> 
>> guard something 
>> guard something
>> allocate memory; defer {release memory}
>> 
>> -- E



More information about the swift-evolution mailing list