[swift-evolution] Proposal: Allow `[strong self]` capture in closures and remove the `self` requirement therein

Greg Parker gparker at apple.com
Tue Dec 15 17:49:19 CST 2015


> On Dec 15, 2015, at 3:18 PM, Drew Crawford <drew at sealedabstract.com> wrote:
> 
> Big -1
> 
> Consider the following
> 
> //somewherelse.swift
> 
> var strongReference: Foo! = nil
> func evil(foo: Foo) {
>     strongReference = foo
> }
> 
> and
> 
> //Foo.swift
> class Bar {
>     var f: Foo
>     dispatch_async(queue) { [strong self] in
>          evil(f)
>     }
> }
> 
> IMHO, `evil(self.f)` is *far* better and *far* more likely to look like a bug.  I feel so strongly about this that I would take the extraordinary step of banning `strong self` from my codebase.  I have spent waaaaaaay too much time playing "spot func evil".

Can you explain what is so evil about func evil() when it is called from an asynchronously-executed closure? I don't see an obvious bug here.


-- 
Greg Parker    gparker at apple.com     Runtime Wrangler


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151215/1d3f0117/attachment.html>


More information about the swift-evolution mailing list