<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="">Actually, the code I proposed does not work. First of all, it doesn't work for subtype relations. This can easily be fixed though by changing `== U.self` to `is U`. More importantly though, it fails when `T` is an existential since `type(of:)` looks within the existential to determine the type. It would be useful if there were a way to determine the *actual* runtime representation of the value.<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 24, 2016, at 6:00 PM, Jaden Geller &lt;<a href="mailto:jaden.geller@gmail.com" class="">jaden.geller@gmail.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="">Taking that suggestion a step further, it's pretty easy to define a function that performs this sort of casting without bridging.<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 14px; 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=""> unbridgedCast&lt;T, U&gt;(</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> x: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">, to: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">U</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Type) -&gt; </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">U</span><span style="font-variant-ligatures: no-common-ligatures" class="">? {</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">guard</span><span style="font-variant-ligatures: no-common-ligatures" class=""> type(of: x) == </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">U</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">unsafeBitCast</span><span style="font-variant-ligatures: no-common-ligatures" class="">(x, to: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">U</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Aug 24, 2016, at 5:42 PM, Xiaodi Wu 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="">import Foundation<br class=""><br class="">let foo: Any = "Hello"<br class="">type(of: foo) == String.self // true<br class="">type(of: foo) == NSString.self // false<br class=""><br class="">let bar: Any = "Hello" as NSString<br class="">type(of: bar) == String.self // false<br class="">type(of: bar) == NSString.self // true<br class=""><br class="">Why not this?<br class=""><br class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Wed, Aug 24, 2016 at 19:09 Charles Srstka via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">MOTIVATION:<div class=""><br class=""></div><div class="">SE-0083 appears to be dead in the water, having been deferred until later in Swift 3 back in May and not having been heard from since then, with the Swift 3 release looming closer and closer. However, the predictability gains that would have been provided by this change remain desirable for cases where one needs to know the actual dynamic type of an entity before any bridging magic is involved. Additionally, performance-critical code may desire the ability to check something’s type quickly without incurring the overhead of Objective-C bridging code.</div><div class=""><br class=""></div><div class="">PROPOSED SOLUTION:</div><div class=""><br class=""></div><div class="">I propose the following operators: really_is, really_as, really_as?, and really_as!. These operators would only return a positive result if the type actually was what was being asked for, instead of something that might be able to bridge to that type.</div><div class=""><br class=""></div><div class="">DETAILED DESIGN:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class="">let</span><span class=""> foo: </span><span style="color:#bb2ca2" class="">Any</span><span class=""> = </span><span style="color:#d12f1b" class="">"Foo"</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class="">let</span><span class=""> bar: </span><span style="color:#bb2ca2" class="">Any</span><span class=""> = </span><span style="color:#703daa" class="">NSString</span><span class="">(string: </span><span style="color:#d12f1b" class="">"Bar"</span><span class="">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class="">let</span><span class=""> fooIsString = </span><span style="color:#4f8187" class="">foo</span><span class=""> </span><span style="color:#bb2ca2" class="">is</span><span class=""> </span><span style="color:#703daa" class="">String &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// true</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#703daa" class=""><span style="color:rgb(187,44,162)" class="">let</span><span style="" class="">&nbsp;fooReallyIsString =&nbsp;</span><span style="color:rgb(79,129,135)" class="">foo</span><span style="" class="">&nbsp;</span><span style="color:rgb(187,44,162)" class="">really_is</span><span style="" class="">&nbsp;</span><span class="">String &nbsp; &nbsp; // true</span></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#703daa" class=""><br class=""></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class="">let</span><span class=""> fooIsNSString = </span><span style="color:#4f8187" class="">foo</span><span class=""> </span><span style="color:#bb2ca2" class="">is</span><span class=""> </span><span style="color:#703daa" class="">NSString &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// true</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#703daa" class=""><span style="color:rgb(187,44,162)" class="">let</span><span style="" class="">&nbsp;fooReallyIsNSString =&nbsp;</span><span style="color:rgb(79,129,135)" class="">foo</span><span style="" class="">&nbsp;</span><span style="color:rgb(187,44,162)" class="">really_is</span><span style="" class="">&nbsp;</span><span class="">NSString // false</span></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class="">let</span><span class=""> barIsString = </span><span style="color:#4f8187" class="">bar</span><span class=""> </span><span style="color:#bb2ca2" class="">is</span><span class=""> </span><span style="color:#703daa" class="">String &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// true</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#703daa" class=""><span style="color:rgb(187,44,162)" class="">let</span><span style="" class="">&nbsp;barReallyIsString =&nbsp;</span><span style="color:rgb(79,129,135)" class="">bar</span><span style="" class="">&nbsp;</span><span style="color:rgb(187,44,162)" class="">really_is</span><span style="" class="">&nbsp;</span><span class="">String &nbsp; &nbsp; // false</span></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#703daa" class=""><br class=""></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class="">let</span><span class=""> barIsNSString = </span><span style="color:#4f8187" class="">bar</span><span class=""> </span><span style="color:#bb2ca2" class="">is</span><span class=""> </span><span style="color:#703daa" class="">NSString &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// true</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#703daa" class=""><span style="color:rgb(187,44,162)" class="">let</span><span style="" class="">&nbsp;barReallyIsNSString =&nbsp;</span><span style="color:rgb(79,129,135)" class="">bar</span><span style="" class="">&nbsp;</span></span><span style="color:rgb(187,44,162)" class="">really_is</span><span class="">&nbsp;</span><span style="color:rgb(112,61,170)" class="">NSString // true</span></div><div class=""><span style="color:#703daa" class=""><br class=""></span></div><div class=""><span style="color:#703daa" class=""><span style="" class="">ALTERNATIVES CONSIDERED:</span></span></div><div class=""><span style="color:#703daa" class=""><span style="" class=""><br class=""></span></span></div><div class=""><span class="">Stick with using an unholy combination of Mirror and unsafeBitCast when you need to know what you’ve actually got.</span></div><div class=""><span class=""><br class=""></span></div><div class=""><span class="">Charles</span></div><div class=""><span class=""><br class=""></span></div></div>_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></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" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></body></html>