<div dir="ltr">I&#39;m still not sure I understand the capture semantics in this proposal. Let&#39;s say we want to do stuff inside NSOperation:<div><br></div><div>class C:NSOperation {</div><div><br></div><div>  func doStuff() {</div><div><br></div><div>    // We want to set the name of new operation to be equal the old name + some suffix that is currently bound to &#39;name&#39;.<br></div><div>    var name = &quot;suffix&quot;</div><div><br></div><div>    // How we do it right now, assume prepareSomething creates a new NSOperation that we want to configure.</div><div><br></div><div>    prepareSomething { operation in</div><div>        <a href="http://operation.name">operation.name</a> = <a href="http://self.name">self.name</a> + name</div><div>    }</div><div><br></div><div>   // How do we do that in the proposal?</div><div><br></div><div><div>    prepareSomething {</div><div>        name = ?</div><div>    }</div><div>}</div></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 11, 2015 at 3:46 PM, Pierre Monod-Broca via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>To answer James:</div><div>Binding is probably a better word than delegation :), it seems more consistent.<div><br></div><div>I’m ok with the javascript syntax of binding, but the curried-like one has the advantage of being consistent with methods eg:</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(195,89,0)"><span style="color:rgb(53,86,138)">let</span><span style="color:rgb(0,0,0)"> f = </span>BarImplementation<span style="color:rgb(0,0,0)">.</span><span style="color:rgb(88,126,168)">doStuff // BarImplementation -&gt; () -&gt; ()</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">f(bar)()</div></div><div><br></div><div>To answer Marc:</div><div>The bound value is not captured from the closure definition site, it’s explicitly bound, usually just before calling the closure. I think a strong reference is enough, like when calling a method. Besides the binding wouldn’t mutate the closure, but create a new one, usually short-lived.</div><div><br></div>I think `self` should keep it’s value, and we should find another word for the bound value, especially to avoid `self` being shadowed. I would suggest `this` if it wasn’t heavily used in other language to mean self.<div><br></div><div><br></div><div><div>
<div>Pierre</div>

</div><div><div class="h5">
<br><div><blockquote type="cite"><div>Le 11 déc. 2015 à 13:36, Marc Knaup &lt;<a href="mailto:marc@knaup.koeln" target="_blank">marc@knaup.koeln</a>&gt; a écrit :</div><br><div><div dir="ltr">How would capture semantics work?<div><br></div><div>I think this could easily lead to reference cycles as the parameter is captured strongly without any hint at the call-site.</div><div>The developer should be warned and have a way to state the capture semantics explicitly through the capture list.</div><div><br></div><div>If the delegate parameter &quot;Bar&quot; becomes &quot;self&quot; in the closure then using &quot;self&quot; everywhere should be required just as in normal closures. &quot;@noescape&quot; would avoid this but the closure reference must be stored then.</div><div>If the delegate parameter &quot;Bar&quot; really becomes &quot;self&quot; there&#39;s also the problem that the original &quot;self&quot; variable suddenly becomes hidden.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 11, 2015 at 1:15 PM, James Campbell via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 11, 2015 at 12:14 PM, James Campbell <span dir="ltr">&lt;<a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">So I don&#39;t like this way of declaring it as it is not very clear what it is doing  but I actually prefer the javascript way of binding, so in your example the code would be this:<div><br></div><div><div style="font-size:12.8px;margin:0px"><font face="Menlo" style="font-size:11px"><span style="color:rgb(53,86,138)">func</span> prepareSomething(setup:() -&gt; ()) {</font></div><span><span><div style="font-size:12.8px;margin:0px"><font face="Menlo" style="font-size:11px"><font color="#cf8724">    </font><span style="color:rgb(53,86,138)">let</span><font color="#cf8724"> </font>bar<font color="#cf8724"> = </font><span style="color:rgb(195,89,0)">BarImplementation</span>()</font></div><div style="font-size:12.8px;margin:0px"><font face="Menlo" style="font-size:11px">    <span style="color:rgb(207,135,36)">// code before</span></font></div></span></span><div style="font-size:12.8px;margin:0px"><font face="Menlo" style="font-size:11px">    setup().bind(bar)</font></div><div style="font-size:12.8px;margin:0px;color:rgb(207,135,36)"><font face="Menlo" style="font-size:11px"><span>    </span>// code after</font></div><div style="font-size:12.8px;margin:0px"><font face="Menlo" style="font-size:11px">}</font></div></div><div style="font-size:12.8px;margin:0px"><font face="Menlo" style="font-size:11px"><br></font></div><div style="font-size:12.8px;margin:0px"><font face="Menlo" style="font-size:11px">The bind method lets Swift know it should bind this to `bar`, so now this is the same as writing `bar`. So in your example:</font></div><div style="font-size:12.8px;margin:0px"><font face="Menlo" style="font-size:11px"><br></font></div><div style="font-size:12.8px;margin:0px"><div style="font-size:12.8px">without delegation </div><div style="font-size:12.8px"><span><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(88,126,168)">prepareSomething<span> { delegate </span><span style="color:rgb(53,86,138)">in</span></div><div style="margin:0px;font-size:11px;font-family:Menlo">    delegate.<span style="color:rgb(88,126,168)">someConfig</span> = <span style="color:rgb(232,35,0)">&quot;Hello World&quot;</span></div><div style="margin:0px;font-size:11px;font-family:Menlo">}</div><div><br></div></span><div>with binding</div><span><div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(88,126,168)">prepareSomething<span> {</span></div><div style="margin:0px;font-size:11px;font-family:Menlo">    <span style="color:rgb(88,126,168)">someConfig</span> = <span style="color:rgb(232,35,0)">&quot;Hello World&quot;</span></div><div style="margin:0px;font-size:11px;font-family:Menlo">}</div></div><div style="margin:0px;font-size:11px;font-family:Menlo"><br></div></span><div style="margin:0px;font-size:11px;font-family:Menlo">These would work the same :) </div></div></div></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 11, 2015 at 11:49 AM, Pierre Monod-Broca <span dir="ltr">&lt;<a href="mailto:pierremonodbroca@gmail.com" target="_blank">pierremonodbroca@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>without delegation </div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(88,126,168)">prepareSomething<span> { delegate </span><span style="color:#35568a">in</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    delegate.<span style="color:#587ea8">someConfig</span> = <span style="color:#e82300">&quot;Hello World&quot;</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div><br></div><div>with delegation</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(88,126,168)">prepareSomething<span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:#587ea8">someConfig</span> = <span style="color:#e82300">&quot;Hello World&quot;</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div></div><div><br></div><div>The exemple is not very compelling, but in a manifest file it would reduce boiler plate.</div><span><font color="#888888"><div><br></div><div>
<div>Pierre</div>

</div></font></span><div><div>
<br><div><blockquote type="cite"><div>Le 11 déc. 2015 à 12:29, James Campbell &lt;<a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a>&gt; a écrit :</div><br><div><div dir="ltr">Whats the advantage over this ?<div><br></div><div><blockquote type="cite" style="color:rgb(80,0,80);font-size:12.8px"><div class="gmail_extra"><div class="gmail_quote"><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 style="word-wrap:break-word"><div><font face="Menlo" style="font-size:11px"><span style="color:rgb(53,86,138)">protocol</span> Bar {</font></div><div><div style="margin:0px"><span><div style="margin:0px"><font face="Menlo" style="font-size:11px">    <span style="color:rgb(53,86,138)">var</span> someConfig: <span style="color:rgb(195,89,0)">String</span> { <font color="#35568a">get set</font> }</font></div><div style="margin:0px"><font face="Menlo" style="font-size:11px">}</font></div><div style="margin:0px;min-height:13px"><font face="Menlo" style="font-size:11px"><br></font></div></span><div style="margin:0px"><font face="Menlo" style="font-size:11px"><span style="color:rgb(53,86,138)">func</span> prepareSomething(setup: (delegate:</font><span style="color:rgb(195,89,0);font-size:11px;font-family:Menlo"> Bar</span><span style="font-size:11px;font-family:Menlo;color:rgb(34,34,34)">) -&gt; ()) {</span></div></div></div></div></blockquote></div></div></blockquote></div></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 11, 2015 at 11:27 AM, Pierre Monod-Broca <span dir="ltr">&lt;<a href="mailto:pierremonodbroca@gmail.com" target="_blank">pierremonodbroca@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div><div><span><blockquote type="cite"><div>Le 11 déc. 2015 à 11:54, James Campbell &lt;<a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a>&gt; a écrit :</div><br><div><div dir="ltr">Could you explain a little more its a bit confusing ?</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 11, 2015 at 10:32 AM, Pierre Monod-Broca via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">A groovy closure can have a delegate which replaces `this` as the default receiver. The issue in groovy is that it is not compatible with static compilation, and there is no way to know from the code what is the type of the delegate.<div><br></div><div><div>It works great for DSL. It would work great the Swift Package Manager manifest, among other things.</div></div><div><br></div><div>It could look like this in swift<br><div><br></div><div><font style="font-size:11px" face="Menlo"><span style="color:rgb(53,86,138)">protocol</span> Bar {</font></div><div><div style="margin:0px;line-height:normal"><div style="margin:0px;line-height:normal"><font style="font-size:11px" face="Menlo">    <span style="color:rgb(53,86,138)">var</span> someConfig: <span style="color:rgb(195,89,0)">String</span> { <font color="#35568a">get set</font> }</font></div><div style="margin:0px;line-height:normal"><font style="font-size:11px" face="Menlo">}</font></div><div style="margin:0px;line-height:normal;min-height:13px"><font style="font-size:11px" face="Menlo"><br></font></div><div style="margin:0px;line-height:normal"><font style="font-size:11px" face="Menlo"><span style="color:rgb(53,86,138)">func</span> prepareSomething(setup: <span style="color:rgb(53,86,138)">@delegate</span> <span style="color:rgb(195,89,0)">Bar</span> -&gt; () -&gt; ()) {</font></div></div></div></div></div></blockquote></div></div></div></blockquote></span>Here we define a function `prepareSomething(_:)` which receives one parameter: a closure that takes a delegate conforming to `Bar`, and otherwise take no parameter and returns nothing</div><div><span><br><blockquote type="cite"><div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><div style="margin:0px;line-height:normal"><div style="margin:0px;line-height:normal"><font style="font-size:11px" face="Menlo"><font color="#cf8724">    </font><span style="color:rgb(53,86,138)">let</span><font color="#cf8724"> </font>bar<font color="#cf8724"> = </font><span style="color:rgb(195,89,0)">BarImplementation</span>()</font></div><div style="margin:0px;line-height:normal"><font style="font-size:11px" face="Menlo">    <span style="color:rgb(207,135,36)">// code before</span></font></div><div style="margin:0px;line-height:normal"><font style="font-size:11px" face="Menlo">    setup(bar)()</font></div></div></div></div></div></blockquote></div></div></div></blockquote></span>Here we pass a delegate to the closure, then call the closure</div><div><span><br><blockquote type="cite"><div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><div style="margin:0px;line-height:normal"><div style="margin:0px;line-height:normal;color:rgb(207,135,36)"><font style="font-size:11px" face="Menlo"><span>    </span>// code after</font></div><div style="margin:0px;line-height:normal"><font style="font-size:11px" face="Menlo">}</font></div><div style="margin:0px;line-height:normal;min-height:13px"><font style="font-size:11px" face="Menlo"><br></font></div><div style="margin:0px;line-height:normal"><font style="font-size:11px" face="Menlo">prepareSomething { () -&gt; () <span style="color:rgb(53,86,138)">in</span></font></div></div></div></div></div></blockquote></div></div></div></blockquote></span>Here we call the function `prepareSomething(_:)` with a closure which we define a the same time</div><div><br><blockquote type="cite"><div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><div style="margin:0px;line-height:normal"><div style="margin:0px;line-height:normal"><font style="font-size:11px" face="Menlo">    someConfig = <span style="color:rgb(232,35,0)">&quot;Hello world&quot;</span></font></div></div></div></div></div></blockquote></div></div></div></blockquote>Here `someConfig` is a property of the closure’s delegate</div><div><span><br><blockquote type="cite"><div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><div style="margin:0px;line-height:normal"><div style="margin:0px;line-height:normal"><font style="font-size:11px" face="Menlo">}</font></div><div style="font-family:Menlo;font-size:11px"><br></div></div><div><div>Where `someConfig` would refer to bar.</div><div><br></div></div></div></div></div></blockquote></div></div></div></blockquote><div><br></div></span>I’m not sure about the syntax, we could also declare the delegate that way, maybe :<div><div style="margin:0px;font-size:11px;line-height:normal"><font face="Menlo"><span style="color:rgb(53,86,138)">func</span> prepareSomething(doSomething: </font><span style="color:rgb(53,86,138);font-family:Menlo">@delegate(</span><span style="color:rgb(195,89,0);font-family:Menlo">Bar</span><span style="color:rgb(53,86,138);font-family:Menlo">)</span><span style="font-family:Menlo"> () -&gt; ()) {</span></div><div style="margin:0px;font-size:11px;line-height:normal"><span style="font-family:Menlo">    </span><span style="color:rgb(207,135,36);font-family:Menlo">/**/</span></div><div style="margin:0px;font-size:11px;line-height:normal"><span style="font-family:Menlo">}</span></div></div><span><div><span style="font-family:Menlo"><br></span></div><blockquote type="cite"><div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><div>
<div>Pierre</div>

</div>

<br></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=xV0JY-2FdZMnUMvSFtZnLiBPRTDDOSQf3-2FpH33HYOlBxG0x8OMTNdemFXqF3foFYoqpGnhR8cy-2FlFL4xK8ijX2ylpZvq90E32A79TBFfiQYS5Pn-2Bo59HipCUsCsV7eMemKd8j14RCY6aiJ8QyHIDjLlmHedoUTG5IACcHSAEiGzeoFzcjBqHCbGp9X00IcqPqaDU-2FFTwIwYZpWABXnqMZQq7KoGvwXM01ndVU3G1gj2uw-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>_______________________________________________<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>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><span style="font-size:16px;line-height:19.2px"></span><span style="font-size:12.8px"> Wizard</span><br></div><div><a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a></div><div><a href="tel:%2B44%207523%20279%20698" value="+447523279698" target="_blank">+44 7523 279 698</a></div></div></div></div></div></div>
</div>
</div></blockquote></span></div><br></div><div><br></div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><span style="font-size:16px;line-height:19.2px"></span><span style="font-size:12.8px"> Wizard</span><br></div><div><a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a></div><div><a href="tel:%2B44%207523%20279%20698" value="+447523279698" target="_blank">+44 7523 279 698</a></div></div></div></div></div></div>
</div>
</div></div></div></blockquote></div><br></div></div></div></div></blockquote></div><div><div><br><br clear="all"><div><br></div>-- <br><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><span style="font-size:16px;line-height:19.2px"></span><span style="font-size:12.8px"> Wizard</span><br></div><div><a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a></div><div><a href="tel:%2B44%207523%20279%20698" value="+447523279698" target="_blank">+44 7523 279 698</a></div></div></div></div></div></div>
</div></div></div>
</div></div></blockquote></div><div><div><br><br clear="all"><div><br></div>-- <br><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><span style="font-size:16px;line-height:19.2px"></span><span style="font-size:12.8px"> Wizard</span><br></div><div><a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a></div><div><a href="tel:%2B44%207523%20279%20698" value="+447523279698" target="_blank">+44 7523 279 698</a></div></div></div></div></div></div>
</div></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=6ZGE61OxINd5lLe2xYh9Ku-2BXbixWNr2nvfzp2IB1sZgWRsPrcN3q2mRgMRDye-2FPR-2FAfm-2BbM-2BAviUEDMpa-2BJOgJNHJhGdFedlvTGnSqTFT7dud7SMuLNBfcWxdZOTWxL4bhebaGbVO0ffZigCO4XVkUwxO30NEgj6cgk1nYD1yqiO4L6NdbdbTOVGByGUKznF4y9aeaNp0n6oN7xPQresh-2Fc27laLObvbdQUVxkXtbsY-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">
<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>
<br></blockquote></div><br></div>
</div></blockquote></div><br></div></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=1p9Jer2O6jVE9KWvo-2B9iUaEyN8slp4IizyiLwsfp54MwjxC9I3k-2BH7-2FuiXZk7cAm7VQAzUsMxS9CqOxZg1y9GhPNbchb1VM1QgMrxSrL51o-2B9hF248ReJGRhUnMYOygxHXPhLq-2Fl-2Fd4-2B1dQ9alpmlMZ4p1snKQMwFaEyC8IxQQKZKh7LWgflefMuuPNtzVfrxshwDaC4AUrnaZZsYxLV1QVnqZ4Zzu86D3NNV6x7338-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>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">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>
<br></blockquote></div><br></div>