<div style="white-space:pre-wrap">Joe, a very good idea to allow directly capturing a value bound to an immutable name without self keyword!<br></div><div class="gmail_quote"><div dir="ltr">On Tue, Dec 8, 2015 at 18:55 Joe Groff via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On Dec 8, 2015, at 7:49 AM, Joe Groff <<a href="mailto:jgroff@apple.com" target="_blank">jgroff@apple.com</a>> wrote:</div><br><div><div style="word-wrap:break-word">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><br></div><div>let x = NSObject()</div><div>dispatch_sync {[weak x] in x!.foo() } // x can already be gone by this point</div><div><br></div><div>There's no one right answer here.</div></div></div></blockquote><br></div></div><div style="word-wrap:break-word"><div>One reason cycles are so prevalent with closures is that it's easy to capture too much, since referencing 'self.foo' always captures the entirety of 'self'. If the properties of 'self' you need are immutable, or you don't need to see their mutations, you can reduce the risk of cycles by capturing those properties explicitly, turning this:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>self.setCallback { doStuffWith(self.zim, self.zang) }</div></blockquote><div><br></div><div>into:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>self.setCallback {[zim, zang] in doStuffWith(zim, zang) }</div><div><br></div></blockquote>For 'let' properties of classes, it'd be reasonable to propose having closures capture the *property* directly by default in this way instead of capturing 'self' (and possibly allowing referencing them without 'self.', since 'self' wouldn't be involved in any cycle formed this way).<div><br></div><div>-Joe<br><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><br></div></blockquote></div></blockquote></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=1p9Jer2O6jVE9KWvo-2B9iUaEyN8slp4IizyiLwsfp54OhmLfIEiLdCxipOBt-2FFf8ebbo4Jsn7ghOpOGxQotCedflNePLVxvOuPSS8lP3GpNuzsq3bAmcRcRJhDGLAAgLfxEJPJUXeEpFkIkaB-2BDR6pW-2FOmz-2B6Z882ANAANAIhLEalwFZgEFvcS2Z3tTrxnv-2Br-2FbLUJVXAWcSBXfqjz49uYtqcyKSIFXLOSKV-2F3WO1YB0-3D" alt="" width="1" height="1" border="0" style="min-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">
</div>
_______________________________________________<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>