[swift-evolution] Pitch: really_is and really_as operators

Xiaodi Wu xiaodi.wu at gmail.com
Wed Aug 24 21:56:03 CDT 2016


On Wed, Aug 24, 2016 at 9:51 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:

> On Wed, Aug 24, 2016 at 9:47 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>
>> Works on Linux :)
>>
>
> And besides, on any platform where `type(of: bar) != NSString`, why should
> you expect `bar really_is NSString == true`? Isn't bar *not really*
> NSString?
>

Ack, I take that back. __NSCFString is a subclass of NSString. Thus, this
works on macOS, as expected:

```
type(of: bar) is NSString.Type // true
type(of: bar) is String.Type // false
```

On Wed, Aug 24, 2016 at 9:44 PM, Charles Srstka <cocoadev at charlessoft.com>
>> wrote:
>>
>>> On Aug 24, 2016, at 7:42 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>>>
>>>
>>> import Foundation
>>>
>>> let foo: Any = "Hello"
>>> type(of: foo) == String.self // true
>>> type(of: foo) == NSString.self // false
>>>
>>> let bar: Any = "Hello" as NSString
>>> type(of: bar) == String.self // false
>>> type(of: bar) == NSString.self // true
>>>
>>> Why not this?
>>>
>>>
>>> Because if you actually try that, it doesn’t work. type(of: bar) ==
>>> NSString.self actually returns false, not true, because the actual instance
>>> belongs to a private subclass of NSString, not NSString itself. type(of:
>>> bar) == NSClassFromString(“__NSCFString”) works, but that doesn’t
>>> really help us.
>>>
>>> Charles
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160824/4672d3a6/attachment.html>


More information about the swift-evolution mailing list