[3.0] SE-0140: Bridge Optionals to nonnull ObjC objects by bridging their payload, or using a sentinel. #4891
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
id-as-Any lets you pass Optional to an ObjC API that takes
nonnull id
, and also lets you bridge containers ofOptional
toNSArray
etc. When this occurs, we can unwrap the value and bridge it so that inhabited optionals still pass into ObjC in the expected way, but we need something to representnone
other than thenil
pointer. Cocoa providesNSNull
as the canonical "null for containers" object, which is the least bad of many possible answers. If we happen to have the rare nested optionalT??
, there is no precedented analog for these in Cocoa, so just generate a unique sentinel object to preserve thenil
-ness depth so we at least don't lose information round-tripping across the ObjC-Swift bridge.Making Optional conform to _ObjectiveCBridgeable is more or less enough to make this all work, though there are a few additional edge case things that need to be fixed up. We don't want to accept
AnyObject??
as an @objc-compatible type, so special-case Optional ingetForeignRepresentable
.Implements SE-0140 (rdar://problem/27905315).