<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Capturing weak by default is not safe. ARC optimization will shrinkwrap the lifetimes of strong references to their last strong use, so capturing a local variable that references a fresh object would cause the object to immediately go away:<div class=""><br class=""></div><div class="">let x = NSObject()</div><div class="">dispatch_sync {[weak x] in x!.foo() } // x can already be gone by this point</div><div class=""><br class=""></div><div class="">There's no one right answer here.</div><div class=""><br class=""></div><div class="">-Joe</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 8, 2015, at 3:15 AM, Andrew Bennett via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">From <a href="https://swift.org/about/" target="_blank" class="">https://swift.org/about/</a>: "The most obvious way to write code should also behave in a safe manner."<div class=""><br class=""></div><div class="">To this end I think that closures should capture references types weakly by default. Pretty much the only way I know of to (easily) create memory issues with pure swift is to capture a strong reference in a closure.</div><div class=""><br class=""></div><div class="">I think with consideration when designing asynchronous APIs this could be quite painless.</div><div class=""><br class=""></div><div class="">Cases weak may be excluded:</div><div class="">&nbsp;* If the closure is @noescape<br class=""></div><div class="">&nbsp;* If the object's lifetime is provably limited to the block</div><div class="">&nbsp;* If it's a value type</div><div class=""><br class=""></div><div class="">I think the upsides by far outweigh the downsides.</div><div class=""><br class=""></div><div class="">Upside:</div><div class="">&nbsp;* no more surprises</div><div class="">&nbsp;* safer code</div><div class=""><br class=""></div><div class="">Downsides:</div><div class="">&nbsp;* You may sometimes have to use optional chaining or similar to resolve a weak reference.</div><div class="">&nbsp;* Beginners need to understand optionals, but they're likely to do so before learning blocks.</div><div class="">&nbsp;* There's probably a few edge cases I haven't explored, and a few more here:<br class=""></div><div class=""><br class=""></div><div class="">class Test {</div><div class="">&nbsp; &nbsp;func doSomething(v: Int) { ... }</div><div class="">&nbsp; &nbsp;func async(callback: Int-&gt;Void) {</div><div class="">&nbsp; &nbsp; &nbsp; doWork { value in</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;callback?(value)</div><div class="">&nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp;}</div><div class="">}</div><div class=""><br class=""></div><div class="">self.test = Test()</div><div class="">self.test.async(test.doSomething) // what is the lifetime of&nbsp;test.doSomething?</div><div class=""><br class=""></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=RoDF4MveSEMYBIqIJA6ub1g8cOZ-2BVYvqV-2FqygPhjPn8teDFBHrRH0e9lbJ9a5EWms73GOgnq-2B0snZ1wVuYckl-2F5Ko7JQHA1b2yq-2FRuIdF2nWLBqgaNGnNbW2J4WCd3I-2BPczFBo9K9SWvRCvJlJXPpCS5VgAEV-2FjN918CZWxXdZrH1aKuJDYtfOWm55XgrktSimAvMBgdeG4WTlt2XJ6p2hgg4ncyey-2FE9ILc41SWeDg-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>