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">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 style="color:rgb(0,0,0)"> fooReallyIsString = </span><span style="color:rgb(79,129,135)">foo</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">really_is</span><span style="color:rgb(0,0,0)"> </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 style="color:rgb(0,0,0)"> fooReallyIsNSString = </span><span style="color:rgb(79,129,135)">foo</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">really_is</span><span style="color:rgb(0,0,0)"> </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 style="color:rgb(0,0,0)"> barReallyIsString = </span><span style="color:rgb(79,129,135)">bar</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">really_is</span><span style="color:rgb(0,0,0)"> </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 style="color:rgb(0,0,0)"> barReallyIsNSString = </span><span style="color:rgb(79,129,135)">bar</span><span style="color:rgb(0,0,0)"> </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 style="color:rgb(0,0,0)">ALTERNATIVES CONSIDERED:</span></span></div><div><span style="color:#703daa"><span style="color:rgb(0,0,0)"><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>_______________________________________________<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/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>