<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="">I’m concerned about the large source compatibility impact this change would have. Even if we claim that it’s only for Swift 4, migration would be difficult. Even if it were just a warning, that could be extremely noisy for large projects.<div class=""><br class=""></div><div class="">What do you propose to mitigate that problem?</div><div class=""><br class=""></div><div class="">I also think that before proceed with this change any further, we should have a lot more data about the impact that it has on real world applications in general. How many places use as for bridging now? What alternative approach would they use? We should see a diff of some of that real world code and see if we actually like the result. I’m not convinced that it is wrong, semantically, to use an as cast to indicate the conversion of a reference type to a value type. As is used to indicate type conversion. Bridging is a side effect, yes, but warning on this change may just feel pedantic without actually improving the situation.</div><div class=""><br class=""></div><div class="">And finally, as you and I have discussed separately, I don’t believe we should confuse the issue of what happens when bridging NSNumber into Swift with the issue of if ‘as’ bridges or not. The Foundation team believes there is a lot of room for improvement in the behavior of ‘as’ casting for NSNumber that would provide a more predictable and safer answer.</div><div class=""><br class=""></div><div class="">- Tony</div><div class=""><br class=""><div class=""><div><blockquote type="cite" class=""><div class="">On Mar 1, 2017, at 8:11 PM, Joe Groff 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=""><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=""><p style="color: rgb(17, 17, 17); font-family: 'Helvetica Neue', Helvetica, Arial, Verdana, sans-serif; word-wrap: break-word; margin: 1.3125em 0px; font-size: 1.1429em; line-height: 1.3125em;" class="">I’d like to investigate separating Objective-C bridging from the behavior of the&nbsp;<code style="line-height: 1;" class="">as</code>/<code style="line-height: 1;" class="">as?</code>/<code style="line-height: 1;" class="">is</code>&nbsp;operator family again for Swift 4. Last year, I proposed&nbsp;<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0083-remove-bridging-from-dynamic-casts.md" style="color: rgb(13, 110, 161); text-decoration: none; transition: color 0.2s ease-in-out; -webkit-transition: color 0.2s ease-in-out;" class="">SE–0083</a>, but we deferred the proposal for lack of time to evaluate its impact. As complicating factors, we now have source compatibility with Swift 3 as a requirement, and the&nbsp;<code style="line-height: 1;" class="">id</code>-as-<code style="line-height: 1;" class="">Any</code>&nbsp;work from&nbsp;<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0116-id-as-any.md" style="color: rgb(13, 110, 161); text-decoration: none; transition: color 0.2s ease-in-out; -webkit-transition: color 0.2s ease-in-out;" class="">SE–0116</a>&nbsp;more or less requires bridging dynamic casts to work. I think we can nonetheless make important improvements in this area in order to simplify the core language and provide more portable behavior across platforms with and without ObjC interop. In retrospect, submitting SE–0083 as an omnibus “fix casting” proposal was a mistake. We can separate out a few smaller subproblems from the overall concept:</p><h2 id="replacingasforbridgingcoercion" style="color: rgb(17, 17, 17); font-size: 27px; line-height: 42px; margin-top: 42px; margin-bottom: 21px; font-family: 'Helvetica Neue', Helvetica, Arial, Verdana, sans-serif;" class="">Replacing&nbsp;<code style="line-height: 1;" class="">as</code>&nbsp;for bridging coercion</h2><p style="color: rgb(17, 17, 17); font-family: 'Helvetica Neue', Helvetica, Arial, Verdana, sans-serif; word-wrap: break-word; margin: 1.3125em 0px; font-size: 1.1429em; line-height: 1.3125em;" class="">Swift 0 shipped with implicit conversions between standard library value types and their bridged Cocoa classes in both directions, and as we’ve eased off of the implicit conversions, we still left the&nbsp;<code style="line-height: 1;" class="">as</code>&nbsp;operator with the ability to force the conversions. This complicates the meaning of&nbsp;<code style="line-height: 1;" class="">as</code>: normally, it just provides type context, but it also has the power to force bridging conversions. These meanings are often at odds:</p><pre style="margin-top: 21px; margin-bottom: 21px; tab-size: 4; color: rgb(17, 17, 17); font-size: 15px; height: 204px; background-color: rgb(248, 248, 248);" class=""><code class="swift hljs" style="line-height: inherit; display: block; overflow-x: auto; padding: 0.5em; color: rgb(51, 51, 51); height: auto;"><span class="hljs-comment" style="color: rgb(153, 153, 136); font-style: italic;">// `NSNumber` is `ExpressibleByIntegerLiteral`, so this gives type context to the literal 0</span>
<span class="hljs-comment" style="color: rgb(153, 153, 136); font-style: italic;">// and is equivalent to `NSNumber(integerLiteral: 0)`</span>
<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span> <span class="hljs-keyword" style="font-weight: bold;">as</span> <span class="hljs-type" style="color: rgb(68, 85, 136); font-weight: bold;">NSNumber</span>

<span class="hljs-comment" style="color: rgb(153, 153, 136); font-style: italic;">// `x` already has type `Int`, so this forces the bridging conversion and is equivalent to</span>
<span class="hljs-comment" style="color: rgb(153, 153, 136); font-style: italic;">// `_bridgeToObjectiveC(x)` (and thereby gives you a different kind of `NSNumber`!)</span>
<span class="hljs-keyword" style="font-weight: bold;">let</span> x: <span class="hljs-type" style="color: rgb(68, 85, 136); font-weight: bold;">Int</span> = <span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>
x <span class="hljs-keyword" style="font-weight: bold;">as</span> <span class="hljs-type" style="color: rgb(68, 85, 136); font-weight: bold;">NSNumber</span></code></pre><p style="color: rgb(17, 17, 17); font-family: 'Helvetica Neue', Helvetica, Arial, Verdana, sans-serif; word-wrap: break-word; margin: 1.3125em 0px; font-size: 1.1429em; line-height: 1.3125em;" class="">Aside from the complexity and non-portability of this behavior, this is also inconsistent with the Swift naming conventions, which recommend that conversions between related types be presented as initializers. Additionally, the bridging conversions often have specialized behavior for performance or semantic reasons that aren’t intended to be exposed in the normal API of either type (for example, bridging a Swift number type to&nbsp;<code style="line-height: 1;" class="">NSNumber</code>&nbsp;produces a “type-preserving” instance of NSNumber so that the bridge doesn’t lose type information, even though&nbsp;<code style="line-height: 1;" class="">NSNumber</code>’s own API presents a type-agnostic numeric object). Therefore, I propose that we remove the bridging behavior from&nbsp;<code style="line-height: 1;" class="">as</code>, and provide APIs for conversion where they don’t yet exist.&nbsp;<code style="line-height: 1;" class="">as</code>&nbsp;is purely a compile-time construct with no runtime interaction, so the Swift 3 compatibility and ABI issues are much simpler than they are when runtime casting behavior becomes involved.</p><h2 id="warningonisasascaststhatstaticallyinducebridging" style="color: rgb(17, 17, 17); font-size: 27px; line-height: 42px; margin-top: 42px; margin-bottom: 21px; font-family: 'Helvetica Neue', Helvetica, Arial, Verdana, sans-serif;" class="">Warning on&nbsp;<code style="line-height: 1;" class="">is</code>/<code style="line-height: 1;" class="">as?</code>/<code style="line-height: 1;" class="">as!</code>&nbsp;casts that statically induce bridging</h2><p style="color: rgb(17, 17, 17); font-family: 'Helvetica Neue', Helvetica, Arial, Verdana, sans-serif; word-wrap: break-word; margin: 1.3125em 0px; font-size: 1.1429em; line-height: 1.3125em;" class="">Without changing the runtime behavior of casting, we could still discourage users from using dynamic casting to perform bridging conversions when it’s statically evident that a bridging conversion is the only way a cast succeeds. For example:</p><pre style="margin-top: 21px; margin-bottom: 21px; tab-size: 4; color: rgb(17, 17, 17); font-size: 15px; height: 120px; background-color: rgb(248, 248, 248);" class=""><code class="swift hljs" style="line-height: inherit; display: block; overflow-x: auto; padding: 0.5em; color: rgb(51, 51, 51); height: auto;"><span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">func</span> <span class="hljs-title" style="color: rgb(153, 0, 0); font-weight: bold;">abuseBridgingCasts</span><span class="hljs-params">(on object: AnyObject)</span></span> {
    <span class="hljs-comment" style="color: rgb(153, 153, 136); font-style: italic;">// warning: dynamic cast requires a bridging conversion; use `Int(bridgedFrom:)` instead</span>
    <span class="hljs-keyword" style="font-weight: bold;">let</span> <span class="hljs-number" style="color: rgb(0, 128, 128);">_</span> = object <span class="hljs-keyword" style="font-weight: bold;">as</span>? <span class="hljs-type" style="color: rgb(68, 85, 136); font-weight: bold;">Int</span>
}</code></pre><p style="color: rgb(17, 17, 17); font-family: 'Helvetica Neue', Helvetica, Arial, Verdana, sans-serif; word-wrap: break-word; margin: 1.3125em 0px; font-size: 1.1429em; line-height: 1.3125em;" class="">This wouldn’t be perfect, since we wouldn’t be able to warn about fully dynamic casts, but it could help encourage users to write portable code that doesn’t rely on the Objective-C bridge in common situations.</p><h2 id="limitingwhentheruntimeallowsbridgingindynamiccasts" style="color: rgb(17, 17, 17); font-size: 27px; line-height: 42px; margin-top: 42px; margin-bottom: 21px; font-family: 'Helvetica Neue', Helvetica, Arial, Verdana, sans-serif;" class="">Limiting when the runtime allows bridging in dynamic casts</h2><p style="color: rgb(17, 17, 17); font-family: 'Helvetica Neue', Helvetica, Arial, Verdana, sans-serif; word-wrap: break-word; margin: 1.3125em 0px; font-size: 1.1429em; line-height: 1.3125em;" class="">Ideally, we would be able to change runtime dynamic casting itself to not involve bridging. However, as I mentioned above, there are at least two situations where bridging dynamic casts are necessary to meet design requirements:</p><ol style="margin-top: 21px; margin-bottom: 21px; padding-left: 1.5em; list-style-position: inside; color: rgb(17, 17, 17); font-family: 'Helvetica Neue', Helvetica, Arial, Verdana, sans-serif; font-size: 15px;" class=""><li style="font-size: 16.5px;" class="">To maintain compatibility with Swift 3 code</li><li style="font-size: 16.5px;" class="">For dynamic-casting&nbsp;<code style="line-height: 1;" class="">Any</code>s that were bridged from an Objective-C&nbsp;<code style="line-height: 1;" class="">id</code>, since we don’t know statically whether a bridge to any particular Swift type is needed</li></ol><p style="color: rgb(17, 17, 17); font-family: 'Helvetica Neue', Helvetica, Arial, Verdana, sans-serif; word-wrap: break-word; margin: 1.3125em 0px; font-size: 1.1429em; line-height: 1.3125em;" class="">However, neither of these is an insurmountable barrier. For Swift 3 compatibility, if nothing else, we could ship a parallel set of runtime entry points for dynamic casting with the Swift 3 behavior that would be used when compiling code in Swift 3 mode (and those entry points could possibly be banished to a separate compatibility dylib to avoid weighing down the ABI-stable Swift runtime forever). For&nbsp;<code style="line-height: 1;" class="">id</code>-as-<code style="line-height: 1;" class="">Any</code>&nbsp;bridging, existentials could potentially carry a bit to indicate whether casting out of that particular value should admit bridging casts. Doing that has complications of its own—having otherwise equivalent&nbsp;<code style="line-height: 1;" class="">Any</code>&nbsp;values have different behavior is undeniably weird—but, if we can keep the behavior isolated to code that directly interfaces with ObjC, I think it’s worth investigating in the interest of making the overall language more predictable.</p><p style="color: rgb(17, 17, 17); font-family: 'Helvetica Neue', Helvetica, Arial, Verdana, sans-serif; word-wrap: break-word; margin: 1.3125em 0px; font-size: 1.1429em; line-height: 1.3125em;" class="">-Joe</p></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></div></body></html>