<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I agree that there are things we could do to improve this situation. My personal preference would be for "implicit closures" like this to be formed as @noescape, and <i class="">require</i>&nbsp;you to explicitly write a closure expression if the closure might escape. But any of these suggestions would count as language changes and should thus be discussed on swift-evolution.</div><div class=""><br class=""></div><div class="">Jordan</div><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 17, 2016, at 13:31, David Turnbull via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Why isn't self unowned by default? Seems like that would fix all kinds of problems including being a novice.<div class=""><br class=""></div><div class=""><div class="">Here's an example of a case where you need a strong self. Imagine that&nbsp;<span style="font-family:Menlo;font-size:11px" class="">[unowned&nbsp;</span><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)" class="">self</span><span style="font-family:Menlo;font-size:11px" class="">]&nbsp;</span>is the default and the programmer made the mistake of forgetting to use&nbsp;<span style="font-family:Menlo;font-size:11px" class="">[strong&nbsp;</span><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)" class="">self</span><span style="font-family:Menlo;font-size:11px" class="">]</span>&nbsp;.</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color:rgb(187,44,162)" class="">class</span> ClosureFactory {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="color:rgb(187,44,162)" class="">let</span> s:<span style="color:rgb(112,61,170)" class="">String</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="color:rgb(187,44,162)" class="">init</span>(<span style="color:rgb(187,44,162)" class="">_</span> s:<span style="color:rgb(112,61,170)" class="">String</span>) { <span style="color:rgb(187,44,162)" class="">self</span>.<span style="color:rgb(79,129,135)" class="">s</span> = s }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="color:rgb(187,44,162)" class="">func</span> writeln() -&gt; () -&gt; <span style="color:rgb(112,61,170)" class="">Void</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:rgb(187,44,162)" class="">return</span> {[unowned <span style="color:rgb(187,44,162)" class="">self</span>] <span style="color:rgb(187,44,162)" class="">in</span> <span style="color:rgb(61,29,129)" class="">print</span>(<span style="color:rgb(187,44,162)" class="">self</span>.<span style="color:rgb(79,129,135)" class="">s</span>)}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color:rgb(187,44,162)" class="">let</span> writer = <span style="color:rgb(79,129,135)" class="">ClosureFactory</span>(<span style="color:rgb(209,47,27)" class="">"hi"</span>).<span style="color:rgb(49,89,93)" class="">writeln</span>()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">writer<span style="" class="">()</span></div></div><div class=""><span style="" class=""><br class=""></span></div><div class=""><span style="" class="">The program crashes. Wouldn't this be better than having a default that makes it easy to write memory leaks? I'd much rather be fixing a bug in code I wrote 5 minutes ago than 5 months ago. Especially a memory leak.</span></div></div><div class=""><span style="" class=""><br class=""></span></div><div class=""><span style="" class="">-david&nbsp;</span><a href="https://github.com/AE9RB/SwiftGL" class="">https://github.com/AE9RB/SwiftGL</a></div><div class=""><span style="" class=""><br class=""></span></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="gmail_extra"><div class="gmail_quote">On Sun, Jan 17, 2016 at 11:57 AM, Jacob Bandes-Storch via swift-users <span dir="ltr" class="">&lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="">I also noticed this problem with nested functions recently:<br class=""><div class=""><div class=""><br class=""></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; class Foo {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; var handler: (() -&gt; Void)?</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; func noop() {}</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; func setup() {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // <b class="">error:</b></font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler = { noop() }</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // <b class="">no error:</b></font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler = noop</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // <b class="">no error:</b></font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; func innerFunc() { noop() }</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler = { innerFunc() }</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler = innerFunc</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; }</font></div></div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class=""><div dir="ltr" class=""><div class="">Jacob<br class=""></div></div></div></div>
<br class=""><div class="gmail_quote"><div class=""><div class="h5">On Sun, Jan 17, 2016 at 11:06 AM, Joshua Scott Emmons via swift-users <span dir="ltr" class="">&lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt;</span> wrote:<br class=""></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class=""><div class="h5"><div style="word-wrap:break-word" class=""><font color="#323333" class="">There's a test case demonstrating what I'm about to talking about:&nbsp;<a href="https://gist.github.com/jemmons/6f668006fa2712a84807" target="_blank" class="">https://gist.github.com/jemmons/6f668006fa2712a84807</a></font><div class=""><font color="#323333" class=""><br class=""></font></div><div class=""><font color="#323333" class="">But here's the nut of it. Given a class like:</font></div><div class=""><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class=""><br class=""></span></div><div class=""><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class=""><span class="">class</span><span style="color:rgb(51,51,51);background-color:rgb(255,255,255)" class=""> Foo {</span>&nbsp;</span></div><div class=""><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">  </span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">var</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class=""> handler: (()</span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">-&gt;</span><span style="color:rgb(0,134,179);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">Void</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class="">)?</span></div><div class=""><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">  func</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class=""> </span><span style="color:rgb(121,93,163);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">noop</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class="">(){}</span></div><div class=""><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class="">  ...</span></div><div class=""><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class="">}</span></div><div class=""><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class=""><br class=""></span></div><div class=""><span style="color:rgb(51,51,51);white-space:pre-wrap;background-color:rgb(255,255,255)" class="">If somewhere in this class I do something like:</span></div><div class=""><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class=""><br class=""></span></div><div class=""><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class="">handler </span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">=</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class=""> { </span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">self</span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">.</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class="">noop() }</span></div><div class=""><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class=""><br class=""></span></div><div class=""><span style="color:rgb(51,51,51);white-space:pre-wrap;background-color:rgb(255,255,255)" class="">I've created a retain cycle because <font face="Menlo" class="">handler</font> holds a strong reference to a closure which holds a strong reference to <font face="Menlo" class="">self</font> which holds a strong reference to a closure that…</span></div><div class=""><span style="color:rgb(51,51,51);white-space:pre-wrap;background-color:rgb(255,255,255)" class=""><br class=""></span></div><div class=""><font color="#333333" class=""><span style="white-space:pre-wrap;background-color:rgb(255,255,255)" class="">In fact, my understanding is Swift's requirement that </span></font><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">self</span><span style="color:rgb(51,51,51);white-space:pre-wrap;background-color:rgb(255,255,255)" class=""> be called out here explicitly is to highlight this danger.&nbsp;</span></div><div class=""><span style="color:rgb(51,51,51);white-space:pre-wrap;background-color:rgb(255,255,255)" class=""><br class=""></span></div><div class=""><span style="color:rgb(51,51,51);white-space:pre-wrap;background-color:rgb(255,255,255)" class="">Swift makes it easy to fix the problem with a capture list:</span></div><div class=""><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class=""><br class=""></span></div><div class=""><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class="">handler </span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">=</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class=""> { [</span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">unowned</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class=""> </span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">self</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class="">] </span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">in</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class=""> </span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">self</span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">.</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class="">noop() }</span></div><div class=""><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class=""><br class=""></span></div><div class=""><font color="#333333" class=""><span style="white-space:pre-wrap;background-color:rgb(255,255,255)" class="">So far, so good. But what if I do something daft like assign <font face="Menlo" class="">noop</font> directly:</span></font></div><div class=""><font color="#333333" class=""><span style="white-space:pre-wrap;background-color:rgb(255,255,255)" class=""><br class=""></span></font></div><div class=""><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class="">handler </span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">=</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class=""> noop</span></div><div class=""><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class=""><br class=""></span></div><div class=""><font color="#333333" class=""><span style="white-space:pre-wrap;background-color:rgb(255,255,255)" class="">This causes a retain loop for all the same reasons as our original example, but there's no&nbsp;</span></font><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">self</span><span style="white-space:pre-wrap;background-color:rgb(255,255,255);color:rgb(51,51,51)" class=""> call-out to warn us of impending doom, and no support for some kind of capture list to fix the issue.</span></div><div class=""><span style="white-space:pre-wrap;background-color:rgb(255,255,255);color:rgb(51,51,51)" class=""><br class=""></span></div><div class=""><span style="background-color:rgb(255,255,255)" class=""><font color="#333333" class=""><span style="white-space:pre-wrap" class="">Is there any possibility of requiring "self" here? As in: "</span></font></span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class="">handler </span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">=</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class="">&nbsp;</span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">self</span><span style="color:rgb(167,29,93);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap" class="">.</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;white-space:pre-wrap;background-color:rgb(255,255,255)" class="">noop" </span><span style="color:rgb(51,51,51);white-space:pre-wrap;background-color:rgb(255,255,255)" class="">for example? And is there any way of specifying how we want self to be retained here?</span></div><div class=""><span style="color:rgb(51,51,51);white-space:pre-wrap;background-color:rgb(255,255,255)" class=""><br class=""></span></div><div class=""><span style="color:rgb(51,51,51);white-space:pre-wrap;background-color:rgb(255,255,255)" class=""><br class=""></span></div><div class=""><span style="color:rgb(51,51,51);white-space:pre-wrap;background-color:rgb(255,255,255)" class="">Cheers,</span></div><div class=""><span style="color:rgb(51,51,51);white-space:pre-wrap;background-color:rgb(255,255,255)" class="">-jemmons</span></div></div><br class=""></div></div></blockquote></div></div></div></blockquote></div><br class=""></div></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>