<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"><<a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a>></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'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:() -> ()) {</font></div><span class=""><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><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 style="color:rgb(0,0,0)"> </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 class=""><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(88,126,168)">prepareSomething<span style="color:rgb(0,0,0)"> { 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)">"Hello World"</span></div><div style="margin:0px;font-size:11px;font-family:Menlo">}</div><div><br></div></span><div>with binding</div><span class=""><div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(88,126,168)">prepareSomething<span style="color:rgb(0,0,0)"> {</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)">"Hello World"</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 class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 11, 2015 at 11:49 AM, Pierre Monod-Broca <span dir="ltr"><<a href="mailto:pierremonodbroca@gmail.com" target="_blank">pierremonodbroca@gmail.com</a>></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 style="color:#000000"> { 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">"Hello World"</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 style="color:#000000"> {</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">"Hello World"</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 <<a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a>> 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"><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><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)">) -> ()) {</span></div></div></div></div></blockquote></div></div></blockquote></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"><<a href="mailto:pierremonodbroca@gmail.com" target="_blank">pierremonodbroca@gmail.com</a>></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 <<a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a>> 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"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></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> -> () -> ()) {</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 { () -> () <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)">"Hello world"</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"> () -> ()) {</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></blockquote></div><br></div></div></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></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><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>+44 7523 279 698</div></div></div></div></div></div>
</div>