Skip to content

[4.2][ObjectiveC] NSObject: Clarify hashing implementation #18258

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

Merged

Conversation

lorentey
Copy link
Member

@lorentey lorentey commented Jul 26, 2018

Cherry picked from #18257

Explanation: NSObject subclasses must customize hashing by overriding the hash property. Provide an explicit, non-overridable definition for NSObject.hash(into:), and update the documentation to explain the contract.
Scope: Objective-C interfaces
Issue: rdar://problem/42623967
Risk: Low. NSObject.hash(into:) is new with Swift 4.2, so making it non-overridable has no source compatibility implications. NSObject.hashValue is left open for now.
Testing: Standard test suite.
Reviewed By: @natecook1000 & @DougGregor (#18257)

NSObject defines its own interface for hashing and equality. Subclasses must implement custom hashing by overriding the hash property, or NSSet and NSDictionary will not work correctly.

Unfortunately, we currently define NSObject.hashValue and NSObject.hash(into:) as overridable, which leads people to accidentally override one of these instead of hash. hashValue is explicitly declared open, while hash(into:) is automatically synthesized as such.

Making hashValue non-open is a potentially source breaking change, but we can at least provide an explicit, non-overridable definition for hash(into:), and update the documentation to explain the contract.

(cherry picked from commit d03786e)
@lorentey
Copy link
Member Author

@swift-ci please test

@lorentey
Copy link
Member Author

@swift-ci please nominate

@airspeedswift airspeedswift merged commit 0190642 into swiftlang:swift-4.2-branch Jul 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants