<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="">I’m a +1 for being able to detect them, a protocol would also be useful for defining methods and such on them in future.</div><div class=""><br class=""></div><div class="">I’m curious why you’re hoping to deny functions in your example though; there’s nothing you can really do with T without testing/casting it, in which case a default case or branch will allow functions to either fall through or throw some kind of unknown type error, e.g-</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> something&lt;T&gt;(x:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span> {</div></div><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">switch</span> (x) {</div></div><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">is</span> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int32</span>:</div></div><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Int32"</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">is</span> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">AnyObject</span>:</div></div><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"AnyObject"</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">default</span>:</div></div><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">fatalError</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span>"Unexpected type"<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; }</div></div><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">}</div></div></blockquote><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">But yeah, either way, being able to specifically detect or require functions/closures would be nice.</span></div><br class=""><div><blockquote type="cite" class=""><div class="">On 31 May 2016, at 11:12, Charlie Monroe via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@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="">AFAIK there isn't currently a way of matching if an Any instance is a closure:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> something&lt;T&gt;(x: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -&gt; </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp;&nbsp;<font color="#008400" class="">/// is x a function? We want to support Any (Int, Int32, Float, Double,</font></span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp;&nbsp;<font color="#008400" class="">///&nbsp;</font></span><span style="color: rgb(0, 132, 0);" class="">&nbsp;</span><span style="color: rgb(0, 132, 0);" class="">AnyObject, ...),&nbsp;</span><span style="color: rgb(0, 132, 0);" class="">but disallow functions.</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; ...</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; min-height: 10px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> myClosure = { </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Hello"</span><span style="font-variant-ligatures: no-common-ligatures" class="">) }</span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">something</span><span style="" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">myClosure</span><span style="" class="">) // Don't allow this.</span></div></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="" class=""><br class=""></span></div><div style="margin: 0px; font-size: 9px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="" class=""><br class=""></span></div><div class="">I propose adding a protocol Function - all closures would conform to it. See the code on&nbsp;</div><div class=""><br class=""></div><div class=""><a href="https://gist.github.com/charlieMonroe/655f2b5e25cc0b4ba06c0ddafa41c73b" class="">https://gist.github.com/charlieMonroe/655f2b5e25cc0b4ba06c0ddafa41c73b</a></div><div class=""><br class=""></div><div class="">which outlines the API and possible usage of it:</div><div class=""><br class=""></div><div class="">- Allowing to match a function from an Any instance.</div><div class="">- Inspect the function object - arguments, captured values, return type. This may help debugging retain cycles by printing the catputred variables - you will be able to see `self` within these.</div><div class="">- Invocation - creating something as NSInvocation, since you'd be able to call the function with a list of arguments.</div><div class="">- This could also become a basis for some RPC in Swift.</div><div class=""><br class=""></div><div class="">I know this is partially something for the Reflection discussion going on here as well as something for the existentials, but since it kind of overlapses both discussions, I thought creating a new thread would perhaps be beneficial.</div><div class=""><br class=""></div><div class="">Charlie</div><div class=""><br class=""></div></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=""></body></html>