<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br><div>This looks more like a reflection api than like strictly speaking conformance. </div><div><br></div><div>But following along, by analogy, to the executeWithArgs, there should be a way to discover the structure of types on AnyClass.</div><div><br></div><div>At this point, it is not the road swift has travelled. Right now they opted for Mirrors to expose the structure of subjects.. all except the functions&closures. At this point Mirrors miss this discoverability, but doing what you propose would make things even more lobsided by having 2 unfinished parts of the language. So I think they don't quite belong today. But runtime reflection goes quite deep in the runtime.</div></div><div><br>On May 31, 2016, at 2:07 PM, Haravikk via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><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<T>(x:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>) -> <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=""> <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=""> <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=""> <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=""> <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=""> <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=""> <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=""> </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=""> }</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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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<T>(x: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </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=""> <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=""> <font color="#008400" class="">/// </font></span><span style="color: rgb(0, 132, 0);" class=""> </span><span style="color: rgb(0, 132, 0);" class="">AnyObject, ...), </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=""> ...</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 </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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>