[swift-evolution] Pitch: really_is and really_as operators

Charles Srstka cocoadev at charlessoft.com
Wed Aug 24 19:08:51 CDT 2016


MOTIVATION:

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.

PROPOSED SOLUTION:

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.

DETAILED DESIGN:

let foo: Any = "Foo"
let bar: Any = NSString(string: "Bar")

let fooIsString = foo is String                  // true
let fooReallyIsString = foo really_is String     // true

let fooIsNSString = foo is NSString              // true
let fooReallyIsNSString = foo really_is NSString // false

let barIsString = bar is String                  // true
let barReallyIsString = bar really_is String     // false

let barIsNSString = bar is NSString              // true
let barReallyIsNSString = bar really_is NSString // true

ALTERNATIVES CONSIDERED:

Stick with using an unholy combination of Mirror and unsafeBitCast when you need to know what you’ve actually got.

Charles

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160824/9a81f453/attachment.html>


More information about the swift-evolution mailing list