<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="">um... how can I cast functions taking one argument to a "generic function pointer" type?<div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="color: rgb(186, 45, 162);" class="">typealias</span> OneArgsFunction&nbsp;= (<span style="color: rgb(186, 45, 162);" class="">Any</span>)-&gt;<span style="color: rgb(186, 45, 162);" class="">Any</span></div><div class=""><span style="color: rgb(186, 45, 162);" class=""><br class=""></span></div><div class=""><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> intToInt(<span style="color: #ba2da2" class="">_</span> i: <span style="color: #703daa" class="">Int</span>) -&gt; <span style="color: #703daa" class="">Int</span> {&nbsp;<span style="color: rgb(186, 45, 162);" class="">return</span> <span style="color: rgb(39, 42, 216);" class="">4&nbsp;</span>}</div></div><div class=""><div class=""><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="color: rgb(186, 45, 162);" class="">func</span>&nbsp;floatToFloat(<span style="color: rgb(186, 45, 162);" class="">_</span>&nbsp;f:&nbsp;<span style="color: rgb(112, 61, 170);" class="">Float</span>) -&gt;&nbsp;<span style="color: rgb(112, 61, 170);" class="">Float</span>&nbsp;{&nbsp;<span style="color: rgb(186, 45, 162);" class="">return</span>&nbsp;0.<span style="color: rgb(39, 42, 216);" class="">4&nbsp;</span>}</div></div></div><div class=""><br class=""></div><div class="">var function :&nbsp;<span style="background-color: rgb(255, 255, 255);" class="">OneArgsFunction</span><span style="background-color: rgb(255, 255, 255);" class="">&nbsp;=&nbsp;</span><span style="background-color: rgb(255, 255, 255);" class="">intToInt</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">function =&nbsp;</span><span style="background-color: rgb(255, 255, 255);" class="">floatToFloat</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><b class="">error: f.playground:4:34: error: cannot convert value of type '(Int) -&gt; Int' to specified type 'OneArgsFunction' (aka '(Any) -&gt; Any')</b></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><b class="">var function : OneArgsFunction = intToInt</b></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><b class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^~~~~~~~</b></span></div><div style="margin: 0px; line-height: normal; min-height: 16px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""></b></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><b class="">error: f.playground:5:12: error: cannot assign value of type '(Float) -&gt; Float' to type 'OneArgsFunction' (aka '(Any) -&gt; Any')</b></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><b class="">function = floatToFloat</b></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><b class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^~~~~~~~~~~~</b></span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><b class=""><br class=""></b></span></div><div class=""><br class=""></div><div class="">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: 'Lucida Grande'; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">--<br class="">C. Keith Ray<br class="">Senior Software Engineer / Trainer / Agile Coach<br class="">* <a href="http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf" class="">http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf</a><br class=""></div><div style="color: rgb(0, 0, 0); font-family: 'Lucida Grande'; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div></div><br class="Apple-interchange-newline">
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Nov 3, 2017, at 2:24 PM, Slava Pestov &lt;<a href="mailto:spestov@apple.com" class="">spestov@apple.com</a>&gt; 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="">Unfortunately we don’t have a way to invoke a function with a runtime argument list because that would require runtime code generation in the most general case.<div class=""><br class=""></div><div class="">I would hack around it by handling the common cases of no arguments, 1 argument, 2 arguments, etc up to some fixed number of arguments that you consider “enough”. Use a switch on the type of the function.</div><div class=""><br class=""></div><div class="">Slava</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Nov 3, 2017, at 2:21 PM, C. Keith Ray via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">In the code below, I'm trying to store a reference to a function with any number of arguments, with any return type. And I want to be able to invoke it.&nbsp;<div class=""><br class=""></div><div class="">Help?<div class=""><br class=""></div><div class="">See the comments with "<span style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 14px; background-color: rgb(255, 255, 255);" class="">*******"</span></div><div class=""><font color="#008400" face="Menlo" class=""><span style="font-size: 14px; background-color: rgb(255, 255, 255);" class=""><br class=""></span></font></div><div class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">typealias</span> Void = ()</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> equalTypes(<span style="color: #ba2da2" class="">_</span> me: [<span style="color: #ba2da2" class="">Any</span>.Type], <span style="color: #ba2da2" class="">_</span> other: [<span style="color: #ba2da2" class="">Any</span>.Type]) -&gt; <span style="color: #703daa" class="">Bool</span> {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">guard</span> me.<span style="color: #703daa" class="">count</span> == other.<span style="color: #703daa" class="">count</span> <span style="color: #ba2da2" class="">else</span> { <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">false</span> }</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 16px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">for</span> i <span style="color: #ba2da2" class="">in</span> <span style="color: #272ad8" class="">0</span> ..&lt; me.<span style="color: #703daa" class="">count</span> {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">if</span> me[i] != other[i] { <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">false</span> }</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; </span>return<span style="" class=""> </span>true</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">struct</span> Function {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> returnType: <span style="color: #ba2da2" class="">Any</span>.Type</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> argTypes: [<span style="color: #ba2da2" class="">Any</span>.Type]</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color: #ba2da2" class="">var</span><span style="" class=""> function: </span><span style="color: #ba2da2" class="">Any</span><span style="" class=""> </span>// ******* any function *******</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 16px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">func</span> perform(<span style="color: #ba2da2" class="">_</span> args: [<span style="color: #ba2da2" class="">Any</span>]) -&gt; <span style="color: #ba2da2" class="">Any</span> {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span>// ******* call function() with args, return result *******</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> <span style="color: #272ad8" class="">0</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 16px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">func</span> isCompatible(<span style="color: #ba2da2" class="">_</span> other: <span style="color: #4f8187" class="">Function</span>) -&gt; <span style="color: #703daa" class="">Bool</span> {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">return</span> <span style="color: #ba2da2" class="">self</span>.<span style="color: #4f8187" class="">returnType</span> == other.<span style="color: #4f8187" class="">returnType</span> &amp;&amp;</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #31595d" class="">equalTypes</span>(<span style="color: #4f8187" class="">argTypes</span>, other.<span style="color: #4f8187" class="">argTypes</span>)</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> vToV() {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color: #3e1e81" class="">print</span><span style="" class="">(</span>"vToV"<span style="" class="">)</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> intToInt(<span style="color: #ba2da2" class="">_</span> i: <span style="color: #703daa" class="">Int</span>) -&gt; <span style="color: #703daa" class="">Int</span> {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color: #3e1e81" class="">print</span><span style="" class="">(</span>"intToInt"<span style="" class="">)</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class=""><span style="" class="">&nbsp; &nbsp; </span>return<span style="" class=""> </span><span style="color: #272ad8" class="">4</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">let</span> f = <span style="color: #4f8187" class="">Function</span>(returnType: <span style="color: #4f8187" class="">Void</span>.<span style="color: #ba2da2" class="">self</span>,</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; argTypes: [],</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function: <span style="color: #31595d" class="">vToV</span>)</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">let</span> r = <span style="color: #4f8187" class="">f</span>.<span style="color: #31595d" class="">perform</span>([])</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129); background-color: rgb(255, 255, 255);" class="">print<span style="" class="">(</span><span style="color: #4f8187" class="">r</span><span style="" class="">)</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">let</span> f2 = <span style="color: #4f8187" class="">Function</span>(returnType: <span style="color: #703daa" class="">Int</span>.<span style="color: #ba2da2" class="">self</span>,</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; argTypes: [<span style="color: #703daa" class="">Int</span>.<span style="color: #ba2da2" class="">self</span>],</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function: <span style="color: #31595d" class="">intToInt</span>)</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">let</span> r2 = <span style="color: #4f8187" class="">f2</span>.<span style="color: #31595d" class="">perform</span>([<span style="color: #272ad8" class="">12</span>])</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129); background-color: rgb(255, 255, 255);" class="">print<span style="" class="">(</span><span style="color: #4f8187" class="">r2</span><span style="" class="">)</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93); background-color: rgb(255, 255, 255);" class=""><span style="color: #3e1e81" class="">assert</span><span style="" class="">(</span><span style="color: #4f8187" class="">f</span><span style="" class="">.</span>isCompatible<span style="" class="">(</span><span style="color: #4f8187" class="">f</span><span style="" class="">))</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93); background-color: rgb(255, 255, 255);" class=""><span style="color: #3e1e81" class="">assert</span><span style="" class="">(!</span><span style="color: #4f8187" class="">f</span><span style="" class="">.</span>isCompatible<span style="" class="">(</span><span style="color: #4f8187" class="">f2</span><span style="" class="">))</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div class="">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="font-family: 'Lucida Grande'; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">--<br class="">C. Keith Ray<br class="">Senior Software Engineer / Trainer / Agile Coach<br class="">* <a href="http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf" class="">http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf</a><br class=""></div><div style="font-family: 'Lucida Grande'; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div></div><br class="Apple-interchange-newline">
</div>

<br class=""></div></div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></body></html>