-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Improve Foundation overlay to handle bridging subscripts and dictionary literals. #3945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Foundation overlay to handle bridging subscripts and dictionary literals. #3945
Conversation
@swift-ci Please smoke test |
extension NSMutableDictionary { | ||
// Bridging subscript. | ||
@nonobjc | ||
final public subscript(key: _Hashable) -> Any? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a FIXME(ABI)
with a note that it should become a generic subscript.
Does this cause any poor interaction with CoreData or other subclassers of NSDictionary? I think the subscript accessors being clean are more important than enforcement of NSCopying (that already is a buyer be aware contract in swift as it is). Also does this properly work for subclasses of NSDictionary made in Swift being handed off into ObjC (subscript is very often accessed in frameworks) |
@phausler Do any of those clients override |
I'm not especially happy with Swift subclassers not being allowed to override the subscript, but as Philippe points out they probably shouldn't be using that as the override point anyway. |
3271f8b
to
9fa724d
Compare
@swift-ci Please smoke test |
@jrose-apple @gribozavr @phausler I tried a different approach that doesn't rely on |
@rudkx @DougGregor Somehow, the overlay changes here trigger a change in behavior in
|
@@ -976,6 +977,18 @@ extension NSDictionary : Sequence { | |||
} | |||
} | |||
|
|||
// Bridging subscript. | |||
@objc | |||
public subscript(key: Any) -> Any? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this need to be @objc
at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…oh, for overriding purposes in Swift today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the only way we get overrideable methods in extensions right now. Without the custom selectors this would collide with the subscript methods from Cocoa.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a FIXME, then?
What's the advantage of using |
That's one reason. |
@jckarter I'll try to look at this next. |
…ry literals. SE-0072 took implicit bridging conversions away, which regressed the ability to express NSDictionaries as dictionary literals and index them using literal keys. Address this by changing the signature of init(dictionaryLiteral:) to use Hashable and Any, and by replacing the subscript from Objective-C with one using _Hashable that does the bridging on the user's behalf. This largely restores the QoI of working with NS collections.
9fa724d
to
62afa03
Compare
@swift-ci Please smoke test and merge |
SE-0072 took implicit bridging conversions away, which regressed the ability to express NSDictionaries as dictionary literals and index them using literal keys. Address this by changing the signature of init(dictionaryLiteral:) to use Hashable and Any, and by replacing the subscript from Objective-C with one using _Hashable that does the bridging on the user's behalf. This largely restores the QoI of working with NS collections.