<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">I could do that, and that would work, but I figured out a way of doing this. </div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style=""><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">import Foundation</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><br></font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">protocol Controller {</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><span class="" style="white-space:pre">        </span>init()</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">}</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><br></font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">class Main : Controller {</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><span class="" style="white-space:pre">        </span>required init() {}</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><span class="" style="white-space:pre">        </span>func index() -&gt; String {</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><span class="" style="white-space:pre">                </span>return &quot;INDEX&quot;</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><span class="" style="white-space:pre">        </span>}</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">}</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><br></font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">let inst = Main()</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">let method = Main.index</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">method(inst)</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><br></font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">func run&lt;T&gt;(ctrl: Controller.Type, method: (T -&gt; () -&gt; String)) {</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><span class="" style="white-space:pre">        </span>let inst = ctrl.init()</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><span class="" style="white-space:pre">        </span>let handler = method(inst as! T)</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><span class="" style="white-space:pre">        </span>print(handler())</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">}</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><br></font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">I&#39;ve posted the source code for the router here: <a href="https://github.com/daviesgeek/swiftrouter">https://github.com/daviesgeek/swiftrouter</a></font></div><div class="gmail_default" style="">It&#39;s very much incomplete, so keep that in mind when you&#39;re looking at it. The relevant code is here: <a href="https://github.com/daviesgeek/swiftrouter/blob/master/Sources/Router.swift#L19">https://github.com/daviesgeek/swiftrouter/blob/master/Sources/Router.swift#L19</a></div><div class="gmail_default" style=""><br></div><div class="gmail_default" style="">Thanks so much for everyone&#39;s help!</div><div class="gmail_default" style=""><br></div></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><br></div><div><div style="color:rgb(136,136,136);font-size:12.8000001907349px;max-width:600px;direction:ltr"><div style="max-width:470px;margin:8px 8px 0px 0px"><table border="0" cellspacing="0" cellpadding="0" width="470" style="width:470px"><tbody><tr valign="top"><td style="font-family:Arial;font-stretch:normal;font-size:14px;color:rgb(100,100,100);padding-left:10px"><div><b>Matthew Davies</b><br>Junior Developer, <a href="http://geostrategies.com" target="_blank">GeoStrategies</a></div><div>Director of Photography, <a href="http://offblockfilms.com" target="_blank">OffBlock Films</a></div><div style="color:rgb(141,141,141);font-size:13px;padding:5px 0px"><a href="tel:209-225.3246" style="color:rgb(141,141,141);text-decoration:none" target="_blank">209-225-3246</a> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="display:inline-block"><a href="tel:209-202-3284" style="color:rgb(141,141,141);text-decoration:none" target="_blank">209-202-3284</a></span> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="display:inline-block"><a href="mailto:daviesgeek@gmail.com" style="color:rgb(141,141,141);text-decoration:none" target="_blank">daviesgeek@gmail.com</a></span> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="white-space:nowrap;display:inline-block"><a href="https://daviesgeek.com/" style="color:rgb(141,141,141);text-decoration:none" target="_blank">daviesgeek.com</a></span></div><div style="margin-top:5px"><a href="http://facebook.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/facebook.png" width="16" height="16"></a> <a href="http://us.linkedin.com/in/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/linkedin.png" width="16" height="16"></a> <a href="http://twitter.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/twitter.png" width="16" height="16"></a> <a href="http://daviesgeek.com/feed.xml" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/blogRSS.png" width="16" height="16"></a>  <a href="http://github.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/github.png" width="16" height="16"></a></div></td></tr></tbody></table></div><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
<br><div class="gmail_quote">On Thu, Dec 10, 2015 at 3:25 PM, Jens Alfke <span dir="ltr">&lt;<a href="mailto:jens@mooseyard.com" target="_blank">jens@mooseyard.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"><span class=""><br><div><blockquote type="cite"><div>On Dec 10, 2015, at 1:01 PM, Matthew Davies via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br><div><div style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">  func get(url: String, ctrl: Controller.Type, method: String) {</div><div style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">    let inst = ctrl.init()</div><div style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><b>    // Run the method that is passed in here</b></div><div style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">  }</div></div></blockquote></div><br></span><div>Since HTTP only has a handful of standard methods/verbs, you can just define a Swift method for each one in your protocol (get, put, delete, etc.) and use a switch statement on the `method` parameter to dispatch to the right one.</div><div><br></div><div>This is a case where you wouldn’t want to use dynamic lookup anyway, since the method name you’d be looking up and calling would be chosen by the remote client. That’s the sort of thing that’s just begging to be exploited (I.e. someone opens a TCP socket and sends “getWithoutCheckingAuth /admin/secretdata.txt HTTP/1.1”…)</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>—Jens</div></font></span></div></blockquote></div><br></div>