<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=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">Le 11 déc. 2015 à 11:54, James Campbell &lt;<a href="mailto:james@supmenow.com" class="">james@supmenow.com</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Could you explain a little more its a bit confusing ?</div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Fri, Dec 11, 2015 at 10:32 AM, Pierre Monod-Broca 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 style="word-wrap:break-word" class="">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 class=""><br class=""></div><div class=""><div class="">It works great for DSL. It would work great the Swift Package Manager manifest, among other things.</div></div><div class=""><br class=""></div><div class="">It could look like this in swift<br class=""><div class=""><br class=""></div><div class=""><font style="font-size:11px" face="Menlo" class=""><span style="color:rgb(53,86,138)" class="">protocol</span> Bar {</font></div><div class=""><div style="margin:0px;line-height:normal" class=""><div style="margin:0px;line-height:normal" class=""><font style="font-size:11px" face="Menlo" class="">&nbsp; &nbsp; <span style="color:rgb(53,86,138)" class="">var</span> someConfig:&nbsp;<span style="color:rgb(195,89,0)" class="">String</span>&nbsp;{&nbsp;<font color="#35568a" class="">get set</font>&nbsp;}</font></div><div style="margin:0px;line-height:normal" class=""><font style="font-size:11px" face="Menlo" class="">}</font></div><div style="margin:0px;line-height:normal;min-height:13px" class=""><font style="font-size:11px" face="Menlo" class=""><br class=""></font></div><div style="margin:0px;line-height:normal" class=""><font style="font-size:11px" face="Menlo" class=""><span style="color:rgb(53,86,138)" class="">func</span> prepareSomething(setup:&nbsp;<span style="color:rgb(53,86,138)" class="">@delegate</span>&nbsp;<span style="color:rgb(195,89,0)" class="">Bar</span> -&gt; () -&gt; ()) {</font></div></div></div></div></div></blockquote></div></div></div></blockquote>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><br class=""><blockquote type="cite" class=""><div class=""><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" class=""><div class=""><div class=""><div style="margin:0px;line-height:normal" class=""><div style="margin:0px;line-height:normal" class=""><font style="font-size:11px" face="Menlo" class=""><font color="#cf8724" class="">&nbsp; &nbsp;&nbsp;</font><span style="color:rgb(53,86,138)" class="">let</span><font color="#cf8724" class=""> </font>bar<font color="#cf8724" class=""> = </font><span style="color:rgb(195,89,0)" class="">BarImplementation</span>()</font></div><div style="margin:0px;line-height:normal" class=""><font style="font-size:11px" face="Menlo" class="">&nbsp; &nbsp;&nbsp;<span style="color:rgb(207,135,36)" class="">// code before</span></font></div><div style="margin:0px;line-height:normal" class=""><font style="font-size:11px" face="Menlo" class="">&nbsp; &nbsp;&nbsp;setup(bar)()</font></div></div></div></div></div></blockquote></div></div></div></blockquote>Here we pass a delegate to the closure, then call the closure</div><div><br class=""><blockquote type="cite" class=""><div class=""><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" class=""><div class=""><div class=""><div style="margin:0px;line-height:normal" class=""><div style="margin:0px;line-height:normal;color:rgb(207,135,36)" class=""><font style="font-size:11px" face="Menlo" class=""><span style="" class="">&nbsp; &nbsp;&nbsp;</span>// code after</font></div><div style="margin:0px;line-height:normal" class=""><font style="font-size:11px" face="Menlo" class="">}</font></div><div style="margin:0px;line-height:normal;min-height:13px" class=""><font style="font-size:11px" face="Menlo" class=""><br class=""></font></div><div style="margin:0px;line-height:normal" class=""><font style="font-size:11px" face="Menlo" class="">prepareSomething { () -&gt; ()&nbsp;<span style="color:rgb(53,86,138)" class="">in</span></font></div></div></div></div></div></blockquote></div></div></div></blockquote>Here we call the function `prepareSomething(_:)` with a closure which we define a the same time</div><div><br class=""><blockquote type="cite" class=""><div class=""><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" class=""><div class=""><div class=""><div style="margin:0px;line-height:normal" class=""><div style="margin:0px;line-height:normal" class=""><font style="font-size:11px" face="Menlo" class="">&nbsp; &nbsp; someConfig =&nbsp;<span style="color:rgb(232,35,0)" class="">"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><br class=""><blockquote type="cite" class=""><div class=""><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" class=""><div class=""><div class=""><div style="margin:0px;line-height:normal" class=""><div style="margin:0px;line-height:normal" class=""><font style="font-size:11px" face="Menlo" class="">}</font></div><div style="font-family:Menlo;font-size:11px" class=""><br class=""></div></div><div class=""><div class="">Where `someConfig` would refer to bar.</div><div class=""><br class=""></div></div></div></div></div></blockquote></div></div></div></blockquote><div><br class=""></div>I’m not sure about the syntax, we could also declare the delegate that way, maybe :<div class=""><div style="margin: 0px; font-size: 11px; line-height: normal;" class=""><font face="Menlo" class=""><span style="color: rgb(53, 86, 138);" class="">func</span>&nbsp;prepareSomething(doSomething:&nbsp;</font><span style="color: rgb(53, 86, 138); font-family: Menlo;" class="">@delegate(</span><span style="color: rgb(195, 89, 0); font-family: Menlo;" class="">Bar</span><span style="color: rgb(53, 86, 138); font-family: Menlo;" class="">)</span><span style="font-family: Menlo;" class="">&nbsp;() -&gt; ()) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;</span><span style="color: rgb(207, 135, 36); font-family: Menlo;" class="">/**/</span></div><div style="margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-family: Menlo;" class="">}</span></div></div><div class=""><span style="font-family: Menlo;" class=""><br class=""></span></div><blockquote type="cite" class=""><div class=""><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" class=""><div class=""><div class=""><div class="">
<div class="">Pierre</div>

</div>

<br class=""></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" class="">
</div>
<br class="">_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" 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/mailman/listinfo/swift-evolution</a><br class="">
<br class=""></blockquote></div><br class=""><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature"><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class=""><span style="font-size:16px;line-height:19.2px" class=""></span><span style="font-size:12.8px" class="">&nbsp;Wizard</span><br class=""></div><div class=""><a href="mailto:james@supmenow.com" target="_blank" class="">james@supmenow.com</a></div><div class="">+44 7523 279 698</div></div></div></div></div></div>
</div>
</div></blockquote></div><br class=""></div><div class=""><br class=""></div></body></html>