<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="">Hello Swift community,<div class=""><br class=""></div><div class="">The review of&nbsp;<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0140-bridge-optional-to-nsnull.md" class="">SE-0140</a>&nbsp;"Bridge Optional As Its Payload Or NSNull” ran from September 2…8, 2016. The proposal is <b class="">accepted</b>&nbsp;with one modification (described below).</div><div class=""><br class=""></div><div class="">Reviews on this proposal were mixed, with many expressing significant concerns about the ability to place an optional value into an ‘Any’, particularly when the ‘Any’ comes from a nonnull-annotated Objective-C API:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// Objective-C</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>@interface MyClass : NSObject</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>- (void)doSomething:(nonnull id)object;</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>@end</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// Swift</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let stringOpt: String? = getSomeString()</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>MyClass().doSomething(stringOpt) &nbsp;// allowed; likely a programmer error</font></div><div class=""><br class=""></div><div class="">This behavior was introduced as part of id-as-Any bridging (<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0116-id-as-any.md" class="">SE-0116</a>). As an <b class="">amendment to SE-0140</b>, Swift will produce a warning when an optional value is converted to a value of type Any, e.g.,</div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>MyClass().doSomething(stringOpt) &nbsp;// warning: optional value of type&nbsp;‘String?’ is converted to an&nbsp;‘Any’</font></div></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">                                        </span>&nbsp; // note: use&nbsp;‘!’ to force-unwrap the optional</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">                                        </span>&nbsp; // note: use&nbsp;‘??’ to provide a default value if the optional is nil</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">                                        </span>&nbsp; // note: use&nbsp;‘as Any’ to silence this warning</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class="">Such a warning will address most accidental injections of optional values into Any, and the core team felt that this addresses accidental boxing of optional values better than leaving the opaque object types to fail fast in Objective-C code that inspects them (e.g., see&nbsp;<a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160905/026961.html" class="">this message</a>&nbsp;for a negative review partly on these grounds).</div><div class=""><br class=""></div><div class="">To the main point of this proposal, which is to bridging to either the payload or NSNull, the core team felt that:</div><div class=""><br class=""></div><div class="">1) Bridging to the payload or NSNull brings to Objective-C code the same behavior that is already present in Swift’s type system, where an optional containing a payload can be dynamically casted to its payload type. For example, this is well-formed in Swift:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>&nbsp;let optString: String? = "hello"<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>&nbsp;let anyValue: Any = optString<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>&nbsp;let stringValue: String = any as! stringValue &nbsp;&nbsp;// downcast succeeds, produces a String</font></div><div class=""><br class=""></div><div class="">2) While NSNull is not widely used in Cocoa APIs, it is better to enable those APIs to work properly when nil optional values do get bridged than to have an opaque-to-Objective-C boxed type that does not work well with any Objective-C APIs.</div><div class=""><br class=""></div><div class="">Thank you to everyone who participated in the review!</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>- Doug</div></body></html>