<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Nov 14, 2017, at 7:31 PM, Howard Lovatt 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="">You can add generic closures with some name mangling and simple transformations, nothing very hard.<div class=""><br class=""></div><div class="">The compiler can emit the following for example:</div><div class=""><br class=""></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px" class="">// User writes<br class="">let increment: &lt;T: Numeric&gt;(T) -&gt; T = { $0 + 1 }<br class="">increment(1) // 2<br class="">increment(1.1) // 2.1<br class=""><br class=""><br class="">// Compiler issues<br class="">struct _Generic_Increment&lt;T: Numeric&gt; { // Mangle name<br class="">&nbsp; &nbsp; let increment: (T) -&gt; T = { $0 + 1 }<br class="">}<br class="">_Generic_Increment&lt;Int&gt;().increment(1) // 2<br class="">_Generic_Increment&lt;Double&gt;().increment(1.1) // 2.1<br class=""></blockquote><br class=""><div class="">It's plausible that the compiler can do better than the above, but the above would be sufficient and is easy to do.</div></div></div></blockquote><div><br class=""></div><div>There are several problems here:</div><div><br class=""></div><div>- You’re adding a new syntax for a typed pattern with a generic parameter list, ‘let foo: &lt;T&gt; …’. Presumably the right hand side of the assignment has to be a closure literal. Also such a literal can only be assigned to a let binding and not passed around as a value, because then you need higher rank types.</div><div><br class=""></div><div>- What happens if you use increment as a value without binding generic parameters? Would that be an error? Eg, ‘let increment2 = increment’.</div><div><br class=""></div><div>- What about where clauses?</div><div><br class=""></div><div>- Right now, nested types cannot capture values from outer scopes, so your proposed lowering would need more work in order to be implemented.</div><div><br class=""></div><div>I’m not sure what this buys you over local functions. Local functions have the advantage that they use the same syntax as ordinary functions, whether they are generic or not. Upon first encountering a local function, someone who has never seen a local function before will immediately know what it does. You’re proposing adding some special cases to the grammar to make local functions look more like closures. This seems to be a step backwards.</div><div><br class=""></div><div>Slava</div><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail_extra"><br clear="all" class=""><div class=""><div class="gmail_signature" data-smartmail="gmail_signature">&nbsp; -- Howard.<br class=""></div></div>
<br class=""><div class="gmail_quote">On 15 November 2017 at 14:02, Matthew Johnson <span dir="ltr" class="">&lt;<a href="mailto:matthew@anandabits.com" target="_blank" class="">matthew@anandabits.com</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="auto" class=""><br class=""><br class=""><div id="m_261741036942948137AppleMailSignature" class="">Sent from my iPhone</div><div class=""><br class="">On Nov 14, 2017, at 6:56 PM, Howard Lovatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class="">Having read all the arguments for what to add to local functions it still strikes me as a poor use of engineering resources to fix them (though I do agree they have problems). A better use of resources would be:<div class=""><br class=""></div><div class="">&nbsp; 1. Deprecate local functions.</div><div class="">&nbsp; 2. Allow closures when assigned to a function type to be:</div><div class="">&nbsp; &nbsp; &nbsp; 2a. Recursive.</div><div class="">&nbsp; &nbsp; &nbsp; 2b. Annotatable with:</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2bi.&nbsp; @inline</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2bii. @escaping</div><div class="">&nbsp; &nbsp; &nbsp; 2c. Generic.</div><div class=""><br class=""></div><div class="">That would be a similar engineering effort and give a better short term result of better closures which would be much more widely applicable as well as addressing the issues with local functions.</div></div></div></blockquote><div class=""><br class=""></div>I believe generic closures would require adding higher rank types to Swift.&nbsp; That would be pretty cool but I suspect the engineering effort is at least an order of magnitude greater than the changes discussed in this thread.<div class=""><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class="">It also gives a better long term result of not having to maintain local functions.</div><div class="">&nbsp;&nbsp;</div></div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class="m_261741036942948137gmail_signature" data-smartmail="gmail_signature">&nbsp; -- Howard.<br class=""></div></div>
<br class=""><div class="gmail_quote">On 15 November 2017 at 09:08, Alex Lynch via swift-evolution <span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@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="">The inference algebra just suggested was enjoyable to read, but is still a new syntax. Which is interesting and deserving of its own proposal. The purpose of this proposal is simply to introduce the existing capture syntax to local functions. Thanks to everyone's feedback pointing out that the `self` reference analysis is a deeper question than initially realized.&nbsp;<div class=""><br class=""></div><div class="">Alex</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote"><div class=""><div class="m_261741036942948137h5">On Tue, Nov 14, 2017 at 4:36 PM, Mike Kluev via swift-evolution <span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@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="m_261741036942948137h5"><div dir="ltr" class=""><span class="">On 14 November 2017 at 21:02, David Hart <span dir="ltr" class="">&lt;<a href="mailto:david@hartbit.com" target="_blank" class="">david@hartbit.com</a>&gt;</span> wrote:<br class=""></span><div class="gmail_extra"><div class="gmail_quote"><span class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space" class=""><br class=""><div class=""><span class="m_261741036942948137m_6043367993829337200m_-5026886634801406416gmail-"><div class=""><br class=""></div></span>I’d be very hesitant to introduce this syntax:</div><div class=""><br class=""></div><div class=""><ul class="m_261741036942948137m_6043367993829337200m_-5026886634801406416gmail-m_2863118398544756606MailOutline"><li class="">it’s new syntax, so it comes with a complexity tax (it isn’t naturally obvious what it means for a func to be weak)</li><li class="">it’s only sugar for the capture of self</li></ul></div></div></blockquote></span><div class="">it might cover well over 90% of use cases (by my "pessimistic" estimate)... if someone has a quick way to scan and analyse, say, github swift sources we may even know that current percentage number of real life usage.</div><span class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class=""><ul class="m_261741036942948137m_6043367993829337200m_-5026886634801406416gmail-m_2863118398544756606MailOutline"><li class="">it doesn’t transpose well to local closures</li></ul></div></div></blockquote><div class=""><br class=""></div></span><div class="">the last one - maybe not. follow me:</div><div class=""><br class=""></div><div class="">let closure = { [weak self, bar] in ... }</div><div class=""><br class=""></div><div class="">which today can be written as:&nbsp;<br class=""></div><div class=""><br class=""></div><div class="">let closure = { [weak self, bar] () -&gt; Int in ... } // full form</div><div class=""><br class=""></div><div class="">or as:</div><div class=""><br class=""></div><div class=""><div class="">let closure: () -&gt; Int = { [weak self, bar] in ... } // full form</div></div><div class=""><br class=""></div><div class=""><div class="">which allows this change:<br class=""></div></div><div class=""><br class=""></div><div class="">let closure:&nbsp;&nbsp;[weak self, bar]&nbsp;() -&gt; Int = { ... } // full alt form</div><div class=""><br class=""></div><div class="">or in alternative form:</div><div class=""><br class=""></div><div class=""><div class="">let closure: &nbsp;weak () -&gt; Int = { [bar] in ... } // short hand form</div></div><div class=""><br class=""></div><div class="">same can be with functions:</div><div class=""><br class=""></div><div class="">func fn() -&gt; Int { [weak self, bar] in ... } // full form</div><div class=""><br class=""></div><div class="">weak func fn() -&gt; Int { [bar] in ... } // short hand form</div><div class=""><br class=""></div><div class="">the two capture attributes will in practice be "close" to each other:</div><div class=""><br class=""></div><div class=""><div class="">weak func fn() {</div><div class="">&nbsp; &nbsp; [bar] in</div><div class="">&nbsp; &nbsp; ....</div><div class="">}</div></div><div class=""><br class=""></div><div class="">and in majority of cases there will be only weak self:</div><div class=""><br class=""></div><div class=""><div class="">weak func fn() {</div><div class="">&nbsp; &nbsp; ....<br class=""></div><div class="">}</div></div><div class=""><br class=""></div><div class="">Mike</div><div class=""><br class=""></div></div></div></div>
<br class=""></div></div><span class="">______________________________<wbr class="">_________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailma<wbr class="">n/listinfo/swift-evolution</a><br class="">
<br class=""></span></blockquote></div><br class=""></div>
<br class="">______________________________<wbr class="">_________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailma<wbr class="">n/listinfo/swift-evolution</a><br class="">
<br class=""></blockquote></div><br class=""></div>
</div></blockquote><blockquote type="cite" class=""><div class=""><span class="">______________________________<wbr class="">_________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a></span><br class=""></div></blockquote></div></div></blockquote></div><br class=""></div>
_______________________________________________<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=""></body></html>