[swift-evolution] [Proposal] Allow using optional binding to upgrade self from a weak to strong reference

Taras Zakharko taras.zakharko at uzh.ch
Sat Feb 20 06:53:20 CST 2016


Even if that would be possible, you’d need to ensure that the RC of the object is not reduced to zero between your check and the end of the closure (that was the criticism with the first version of Evan’s proposal). I also read your comment sin the other thread and they seem quite focused on the unowned self case. I am not sure that its that frequent. When you sue closures for callbacks and/or signals, using weak self makes more sense to me. Another problem I have with the alternative proposal (guard self in the capture list) is that it does not offer an obvious way to remove the closure once the object leaves the scope. Imagine that I am using closures for some sort of observing or notification mechanism. There can potentially be many short-lived objects who receive notifications from a single source. With Evan’s proposal, I can do something like this:

guard self = self  else  { remove the current closure from the notification list; return }

I don’t see an obvious way to accomplish this in the alternative proposal. 

Best, 

 Taras 


> On 20 Feb 2016, at 08:52, Kurt Werle <kurt at circlew.org> wrote:
> 
> Arg!  My mistake - I did not realize you can't test an unowned to be nil like you can an implicitly unwrapped variable.
> 
> On Fri, Feb 19, 2016 at 11:03 PM, Shawn Erickson <shawnce at gmail.com <mailto:shawnce at gmail.com>> wrote:
> True, however only if unowned works in terms of the desired object life time / "ownership" for self. It isn't always sufficient so weak is needed to avoid the retain cycle while safely supporting self becoming nil (e.g. allowing the object referenced by self to go away while the block may still be outstanding). The proposal outlines at least one example of that need and is written assuming you need to use weak.
> 
> On Fri, Feb 19, 2016 at 10:15 PM Kurt Werle <kurt at circlew.org <mailto:kurt at circlew.org>> wrote:
> On Fri, Feb 19, 2016 at 9:04 PM, Evan Maloney <emaloney at gilt.com <mailto:emaloney at gilt.com>> wrote:
> 
> ... 
> With this feature, the code above could be rewritten as:
> 
> networkRequest.fetchData() { [weak self] result in
>     guard let self = self else { return }
> If you're going to have boilerplate like that, then the following seems like it accomplishes the same thing [in this specific case] without changing anything:
> 
> networkRequest.fetchData() { [unowned self] result in
>     guard self != nil else { return }
>  
> Kurt
> -- 
> kurt at CircleW.org
> http://www.CircleW.org/kurt/ <http://www.circlew.org/kurt/>
> 
> 
> 
> -- 
> kurt at CircleW.org
> http://www.CircleW.org/kurt/ <http://www.circlew.org/kurt/>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160220/42d6db27/attachment.html>


More information about the swift-evolution mailing list