[swift-evolution] Proposal: weakStrong self in completion handler closures

Davide De Franceschi defrenz98 at hotmail.com
Sun Dec 6 04:01:53 CST 2015


I agree that "weak-strong" is a commonly used (and correct) pattern, and I also wish that there were an easier way to do that. I also did file a radar myself about that.

But I don't have any idea of the correct way of implementing it because the actually desired behaviour in the "failing" case can change depending on your use-case.

Most of the times you just want to `guard let strongSelf = self else { return }`, but that works when you return `Void` and you don't want to manage this "failure" in any other way...
Should `weak-strong` be allowed only for closures that `-> Void`?
What if you want to fire a "completionBlock" anyway?

While the need to remove this common boilerplate feels strong, I'm not certain this would be the right way. Can't really suggest a better alternative though.


> On 5 Dec 2015, at 19:21, Jacob Bandes-Storch via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Another approach would be to simply allow
> 
> guard let self = self else { return }
> On Sat, Dec 5, 2015 at 3:48 AM Robert Vojta <rvojta at me.com <mailto:rvojta at me.com>> wrote:
> Hi all,
> 
> let’s say we have a completion handler closure for some function (networking, …) and we have [weak self] there. Example …
> 
> doSomething() { [weak self] result in
>> }
> 
> … then we can use self?.whatever to access self properties, methods. Or we can try to check if self exists ...
> 
> guard let strongSelf = self else { return }
> 
> … and use strongSelf.
> 
> Can we introduce [weakStrong self] with following behavior:
> 
>  - self is a weak reference
>  - when the closure is going to be executed, all weakStrong weak references are checked if they do exist
>  - if they do exist, they’re strong referenced for the closure and the closure is executed
>  - if they don’t exist, closure is not executed
> 
> doSomething() { [weakStrong self] result in
>   // Closure code is not executed if self no longer exists
>   // self is a strong reference now
> }
> 
> What do you think? Does it make sense?
> 
> My motivation is to get rid off of the repetitive code like this one:
> 
> doSomething() { [weak self] result in
>   guard let strongSelf = self else { return }
>   strongSelf.doSomethingWithResult(result)
> }
> 
> Robert
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>  _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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


More information about the swift-evolution mailing list