<div dir="ltr">I used this several months ago for extracting data from Any:<div><pre style="color:rgb(2,52,116);background-color:rgb(249,248,245)">// Dynamic cast without bridging
func dynamicCast&lt;T&gt;(_ type: T.Type, _ v: Any)-&gt;T? {
    guard let result = v as? T where v.dynamicType is T.Type else {
        return nil;
    }
    return result
}</pre><pre style="color:rgb(2,52,116);background-color:rgb(249,248,245)">from <a href="http://www.callionica.com/developer/#any-equals">http://www.callionica.com/developer/#any-equals</a></pre><pre style="color:rgb(2,52,116);background-color:rgb(249,248,245)">based on suggestion from Joe Groff</pre></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Aug 24, 2016 at 6:11 PM, Jaden Geller via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</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">Though this performs an *extra* check over just `x as! U`, it avoids bridging conversions. I don&#39;t think this extra check matters though because you wouldn&#39;t be using `Any` in code that&#39;s performance critical enough for this to matter, so I&#39;m currently -1 on this proposal.<div><div class="h5"><div><br><div><blockquote type="cite"><div>On Aug 24, 2016, at 6:09 PM, Jaden Geller &lt;<a href="mailto:jaden.geller@gmail.com" target="_blank">jaden.geller@gmail.com</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word">Scratch that last message. Determining what&#39;s stored in `Any` was Charles&#39;s original goal, and what I said made no sense. This should work:<div><br></div><div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">func</span><span> unbridgedCast&lt;T, U&gt;(</span><span style="color:#bb2ca2">_</span><span> x: </span><span style="color:#4f8187">T</span><span>, to: </span><span style="color:#4f8187">U</span><span>.Type) -&gt; </span><span style="color:#4f8187">U</span><span>? {</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#bb2ca2">guard</span><span> type(of: x) </span><span style="color:#bb2ca2">is</span><span> </span><span style="color:#4f8187">U</span><span>.Type </span><span style="color:#bb2ca2">else</span><span> { </span><span style="color:#bb2ca2">return</span><span> </span><span style="color:#bb2ca2">nil</span><span> }</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#bb2ca2">return</span><span> x </span><span style="color:#bb2ca2">as</span><span>! </span><span style="color:#4f8187">U</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span><br></span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span><span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> x = <span style="color:#31595d">unbridgedCast</span>(<span style="color:#4f8187">x</span>, to: <span style="color:#703daa">String</span>.<span style="color:#bb2ca2">self</span>) { ... }</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span><br></span></div><div><blockquote type="cite"><div>On Aug 24, 2016, at 6:06 PM, Jaden Geller &lt;<a href="mailto:jaden.geller@gmail.com" target="_blank">jaden.geller@gmail.com</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word">Actually, the code I proposed does not work. First of all, it doesn&#39;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><br><div><blockquote type="cite"><div>On Aug 24, 2016, at 6:00 PM, Jaden Geller &lt;<a href="mailto:jaden.geller@gmail.com" target="_blank">jaden.geller@gmail.com</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word">Taking that suggestion a step further, it&#39;s pretty easy to define a function that performs this sort of casting without bridging.<div><br></div><div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">func</span><span> unbridgedCast&lt;T, U&gt;(</span><span style="color:#bb2ca2">_</span><span> x: </span><span style="color:#4f8187">T</span><span>, to: </span><span style="color:#4f8187">U</span><span>.Type) -&gt; </span><span style="color:#4f8187">U</span><span>? {</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#bb2ca2">guard</span><span> type(of: x) == </span><span style="color:#4f8187">U</span><span>.</span><span style="color:#bb2ca2">self</span><span> </span><span style="color:#bb2ca2">else</span><span> { </span><span style="color:#bb2ca2">return</span><span> </span><span style="color:#bb2ca2">nil</span><span> }</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#bb2ca2">return</span><span> </span><span style="color:#3d1d81">unsafeBitCast</span><span>(x, to: </span><span style="color:#4f8187">U</span><span>.</span><span style="color:#bb2ca2">self</span><span>)</span></div><div style="margin:0px;font-size:14px;line-height:normal;font-family:Menlo"><span>}</span></div></div><div><br><div><blockquote type="cite"><div>On Aug 24, 2016, at 5:42 PM, Xiaodi Wu via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div>import Foundation<br><br>let foo: Any = &quot;Hello&quot;<br>type(of: foo) == String.self // true<br>type(of: foo) == NSString.self // false<br><br>let bar: Any = &quot;Hello&quot; as NSString<br>type(of: bar) == String.self // false<br>type(of: bar) == NSString.self // true<br><br>Why not this?<br><br><br><div class="gmail_quote"><div dir="ltr">On Wed, Aug 24, 2016 at 19:09 Charles Srstka via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br></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">MOTIVATION:<div><br></div><div>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><br></div><div>PROPOSED SOLUTION:</div><div><br></div><div>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><br></div><div>DETAILED DESIGN:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">let</span><span> foo: </span><span style="color:#bb2ca2">Any</span><span> = </span><span style="color:#d12f1b">&quot;Foo&quot;</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">let</span><span> bar: </span><span style="color:#bb2ca2">Any</span><span> = </span><span style="color:#703daa">NSString</span><span>(string: </span><span style="color:#d12f1b">&quot;Bar&quot;</span><span>)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">let</span><span> fooIsString = </span><span style="color:#4f8187">foo</span><span> </span><span style="color:#bb2ca2">is</span><span> </span><span style="color:#703daa">String                  // true</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#703daa"><span style="color:rgb(187,44,162)">let</span><span> fooReallyIsString = </span><span style="color:rgb(79,129,135)">foo</span><span> </span><span style="color:rgb(187,44,162)">really_is</span><span> </span><span>String     // true</span></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#703daa"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">let</span><span> fooIsNSString = </span><span style="color:#4f8187">foo</span><span> </span><span style="color:#bb2ca2">is</span><span> </span><span style="color:#703daa">NSString              // true</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#703daa"><span style="color:rgb(187,44,162)">let</span><span> fooReallyIsNSString = </span><span style="color:rgb(79,129,135)">foo</span><span> </span><span style="color:rgb(187,44,162)">really_is</span><span> </span><span>NSString // false</span></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">let</span><span> barIsString = </span><span style="color:#4f8187">bar</span><span> </span><span style="color:#bb2ca2">is</span><span> </span><span style="color:#703daa">String                  // true</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#703daa"><span style="color:rgb(187,44,162)">let</span><span> barReallyIsString = </span><span style="color:rgb(79,129,135)">bar</span><span> </span><span style="color:rgb(187,44,162)">really_is</span><span> </span><span>String     // false</span></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#703daa"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">let</span><span> barIsNSString = </span><span style="color:#4f8187">bar</span><span> </span><span style="color:#bb2ca2">is</span><span> </span><span style="color:#703daa">NSString              // true</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#703daa"><span style="color:rgb(187,44,162)">let</span><span> barReallyIsNSString = </span><span style="color:rgb(79,129,135)">bar</span><span> </span></span><span style="color:rgb(187,44,162)">really_is</span><span> </span><span style="color:rgb(112,61,170)">NSString // true</span></div><div><span style="color:#703daa"><br></span></div><div><span style="color:#703daa"><span>ALTERNATIVES CONSIDERED:</span></span></div><div><span style="color:#703daa"><span><br></span></span></div><div><span>Stick with using an unholy combination of Mirror and unsafeBitCast when you need to know what you’ve actually got.</span></div><div><span><br></span></div><div><span>Charles</span></div><div><span><br></span></div></div>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
</blockquote></div>
______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></div></blockquote></div><br></div></div></div></blockquote></div><br></div></div></div></blockquote></div><br></div></div></div></blockquote></div><br></div></div></div></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div>