[swift-evolution] [Proposal draft] Bridge Optional As Its Payload Or NSNull

Charles Srstka cocoadev at charlessoft.com
Wed Aug 24 23:35:51 CDT 2016


> On Aug 24, 2016, at 10:40 PM, jaden.geller at gmail.com wrote:

> 
> Why is exposing an Optional as an opaque box less error prone than an NSNull? That doesn't seem obviously true to me.

Firstly, to answer your question: It is less error-prone because the error is more obvious. If an Objective-C API is passed an array of optionals, resulting in a bunch of completely useless opaque objects, whatever was intended in sending that array will likely fail completely in a way that will almost certainly be discovered and fixed before the product ships. If we implement this proposal, however, it is probable that arrays of optionals will be sent to Objective-C APIs accidentally, and this error can slip past testing if it turns out that all of the elements in the array are usually all .Some, with nil only appearing in unusual edge cases. This can lead to NSNull showing up in contexts where it was not expected at all, leading to crashes in shipping code that would appear very mysterious and difficult to debug. NSNull seems to me to be the sort of thing that should only ever put in as a deliberate and conscious choice on the part of the developer.

Secondly, even if you disregard all of the above, the burden of proof in these matters should be on the side proposing the change, and it does not seem evident to me that NSNull would be less error-prone than the optional box.

Charles



More information about the swift-evolution mailing list