<div dir="ltr">When you use &quot;weak self&quot;, self becomes Optional inside the closure, and indeed it *can* become nil partway through. This is the same as __weak in Objective-C, and there&#39;s actually a clang warning for it (search for &quot;repeated-use-of-weak&quot; or see <a href="http://devstreaming.apple.com/videos/wwdc/2013/404xbx2xvp1eaaqonr8zokm/404/404.pdf" target="_blank">http://devstreaming.apple.com/videos/wwdc/2013/404xbx2xvp1eaaqonr8zokm/404/404.pdf</a>).<div><br></div><div>That&#39;s why &quot;guard let strongSelf = self&quot; is important, and I think &quot;guard let self = self&quot; would add value without adding confusion.</div><div><br></div><div>The current behavior is easily demonstrated:</div><div><br></div><div><div>import Foundation</div><div><br></div><div>class C {</div><div>    func doStuff() {</div><div>        dispatch_async(dispatch_get_global_queue(0, 0)) { [weak self] in</div><div>            print(&quot;self is \(self), waiting...&quot;)</div><div>            usleep(500_000)</div><div>            print(&quot;self is \(self), waiting...&quot;)</div><div>            usleep(500_000)</div><div>            print(&quot;self is \(self)&quot;)</div><div>        }</div><div>    }</div><div>}</div><div><br></div><div>var obj: C? = C()</div><div>obj!.doStuff()</div><div><br></div><div>usleep(750_000)</div><div>print(&quot;main thread - releasing&quot;)</div><div>obj = nil</div><div><br></div><div>sleep(2)</div></div><div><br></div><div><div><br></div></div><div class="gmail_extra">
<br><div class="gmail_quote">On Sat, Dec 5, 2015 at 1:24 PM, Javier Soto <span dir="ltr">&lt;<a href="mailto:javier.api@gmail.com" target="_blank">javier.api@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">My understanding was that [weak variable] behaved that way in Swift already (but now I&#39;m not certain and can&#39;t find a definitive answer on the book), creating a strong reference. Otherwise this turns into inconsistent behavior, where the variable could deallocate and become nil at any point inside the closure. <br><br>Those semantics are also important in a case with a closure within a closure:<br><br>function1(){  [weak self] in<br> function2() {<br>  // the value (If present) inside Optional&lt;Self&gt; is a strong reference here<br> }<br>}<br><div class="gmail_quote"><div><div><div dir="ltr">On Sat, Dec 5, 2015 at 11:22 AM Jacob Bandes-Storch via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br></div></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div>Another approach would be to simply allow<br><br>guard let self = self else { return }<br><div class="gmail_quote"><div dir="ltr">On Sat, Dec 5, 2015 at 3:48 AM Robert Vojta &lt;<a href="mailto:rvojta@me.com" target="_blank">rvojta@me.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi all,<br>
<br>
let’s say we have a completion handler closure for some function (networking, …) and we have [weak self] there. Example …<br>
<br>
doSomething() { [weak self] result in<br>
  …<br>
}<br>
<br>
… then we can use self?.whatever to access self properties, methods. Or we can try to check if self exists ...<br>
<br>
guard let strongSelf = self else { return }<br>
<br>
… and use strongSelf.<br>
<br>
Can we introduce [weakStrong self] with following behavior:<br>
<br>
 - self is a weak reference<br>
 - when the closure is going to be executed, all weakStrong weak references are checked if they do exist<br>
 - if they do exist, they’re strong referenced for the closure and the closure is executed<br>
 - if they don’t exist, closure is not executed<br>
<br>
doSomething() { [weakStrong self] result in<br>
  // Closure code is not executed if self no longer exists<br>
  // self is a strong reference now<br>
}<br>
<br>
What do you think? Does it make sense?<br>
<br>
My motivation is to get rid off of the repetitive code like this one:<br>
<br>
doSomething() { [weak self] result in<br>
  guard let strongSelf = self else { return }<br>
  strongSelf.doSomethingWithResult(result)<br>
}<br>
<br>
Robert<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>
</div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=8K1sNvSH2KR-2BkHEodrUTpXfk2GzibLlIRii1pWdIrM-2Bd6C1U86-2BIQFlrBV4QHYIwO4Ju7QuYG05dKhl-2B0k7XX2zWjNk12mu9eI5NPqUZCO5bL6q8M9xA3UKbcHrGX-2BNunL9WE3ZYTgyPr4X9lemxGfvXzxxAzAgOH6nPd2CeAIrU-2Fa0Ymmb3E-2BAdKbQpm5rSkVJileTlnBuQjLeFn5qXi58Qrnrss3OZr4rqgxI3-2F1w-3D" alt="" width="1" height="1" border="0" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;">
_______________________________________________<span><br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</span></blockquote></div><span><font color="#888888"><div dir="ltr">-- <br></div>Javier Soto
</font></span></blockquote></div><br></div></div>