<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="">The other concern with patterns like this is that they tend to lead to reference cycles due to unintentional closure capture.<div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>David</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 4, 2015, at 11:25 AM, Cole Kurkowski <<a href="mailto:crk@fastmail.com" class="">crk@fastmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I agree that a closure is a much better solution to the target-action pattern in Swift, especially since you could pass a declared method with “self.functionToCall” which is basically a type safe version of the current paradigm. All of that said, I believe the reason it <b class="">doesn’t</b> work this way right now is compatibility with objective-c and Cocoa. I’d love to see this changed, as it’s one of the least Swifty things I have to use on a regular basis. I’m just not sure exactly how large of an effort it would be to make it compatible.<div class=""><br class=""></div><div class=""><div class="">Thanks for your time,</div><div class="">Cole Kurkowski</div><div class=""><blockquote type="cite" class=""><div class="">On Dec 4, 2015, at 12:49, Chris Byatt <<a href="mailto:byatt.chris@gmail.com" class="">byatt.chris@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">It feels a lot more Swift-like to me to allow a Selector argument to have the option of being a closure.<div class=""><br class=""></div><div class="">For example, instead of:</div><div class=""><br class=""></div><div class=""><pre class="default prettyprint prettyprinted" style="margin-top:0px;padding:5px;border:0px;font-size:13px;overflow:auto;width:auto;max-height:600px;background-color:rgb(238,238,238);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif;color:rgb(57,51,24);word-wrap:normal"><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif;white-space:inherit" class=""><span class="typ" style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">UIBarButtonItem</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">title</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="str" style="margin:0px;padding:0px;border:0px;color:rgb(128,0,0)">"Press"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> style</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">.</span><span class="typ" style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">Done</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> target</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="kwd" style="margin:0px;padding:0px;border:0px;color:rgb(0,0,139)">self</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> action</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="str" style="margin:0px;padding:0px;border:0px;color:rgb(128,0,0)">"functionToCall"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
func functionToCall</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">()</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="com" style="margin:0px;padding:0px;border:0px;color:rgb(128,128,128)">// Do something</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">}</span></code></pre>We should be able to do:</div><div class=""><br class=""></div><div class=""><pre class="default prettyprint prettyprinted" style="margin-top:0px;padding:5px;border:0px;font-size:13px;overflow:auto;width:auto;max-height:600px;background-color:rgb(238,238,238);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif;color:rgb(57,51,24);word-wrap:normal"><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif;white-space:inherit" class=""><span class="typ" style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">UIBarButtonItem</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">title</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="str" style="margin:0px;padding:0px;border:0px;color:rgb(128,0,0)">"Press"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> style</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">.</span><span class="typ" style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">Done</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> target</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="kwd" style="margin:0px;padding:0px;border:0px;color:rgb(0,0,139)">self</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> action</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="com" style="margin:0px;padding:0px;border:0px;color:rgb(128,128,128)">// Do Something</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">})</span></code></pre>For simple tasks it would be a lot neater, and indeed faster to code this way.</div><div class=""><br class=""></div><div class="">Thanks,</div><div class=""><br class=""></div><div class="">Chris</div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=-2Bxb47WeGoqMoAgzLM1ObomDZ33eAoWUyfgnPK0aY0klfX6GZZ0U1azyZ2dd2hgKI8V3ZmXOfIqZZbLKXQe0g8H5wxrB4kdW8ZkmhNJZ7Wpf7tssfgijJOSiFA1sVv5574-2FeIxiTssST5MNhN1S1-2FUYVFCpCXsvHgaoWuj3LvCZPJ1p8dVO8o1UQMTjSzkkCzvQ-2FK24I-2Bpo9xhQ-2BH1nufcC3ZbTeN9K7aBeG53-2B-2BWvm8-3D" alt="" width="1" height="1" border="0" style="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="">
_______________________________________________<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" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=aEV7ctwDx5JMbO5IT0R-2F-2Fi-2FIc4mR8uN1H-2FRPAs6LeFcmMUlNju-2F-2B5hmwB7pBEd-2BAsjTZtkAsYVyQ-2BCVYXeJfWma5RZKVDGSMoteNoXpVUmYcub-2BiEa8Ux5va6KK7LTqSB5xNEolr0OVm9eJEWjxQozpmRVvqBC0TxTMk2PE0Qgv2Q9FGz-2Fvvcj6nB4-2BKWdtvNAyfauoVMYU4GSLGVesEBhf5CH9IySdMFJrNuLJO1WE-3D" alt="" width="1" height="1" border="0" style="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="">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=""></div></body></html>