-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Foundation] Measurement: Add a correct hash(into:) implementation #20879
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
Conversation
The new definition normalizes measurements with dimensional units to their base unit, ensuring that the generated hash values are consistent with the definition of ==. It also feeds the unit to the hasher, not just the numerical value. The old hashValue definition was invalid, and has been removed. (I.e., replaced by a compiler-supplied implementation that works in terms of hash(into:).) rdar://problem/46187171
(Tests to cover hashing are coming soon.) |
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.
We won't be able to make this change on its own — we have to maintain hash values identically between Measurement
and NSMeasurement
for bridging; with just this side, measurements are going to disappear from bridged sets and similar.
We should probably coordinate matching changes within Foundation at the same time.
Hm, good point @itaiferber. I was going to suggest also making sure the change made its way to swift-corelibs too. |
We should probably have the implementation also match equality exactly: |
I wonder if we can actually change the definition of equality on either side in a source compatible way. |
@itaiferber @parkera There is no need to ever match hash values between structs defined in the Swift overlay and the corresponding Cocoa classes, even if they're bridged. Values get rehashed during bridging when necessary. Swift and Cocoa have very different approaches to hashing -- starting with the fact that Swift's hash values are explicitly unstable across executions, since they're randomly seeded. Trying to preserve specific hash values for these types is therefore unnecessary. Differences in the definition of equality are more unfortunate, but String and NSString manages to (mostly) make it work. If Note that Swift deprecated |
To clarify: the issue only affects Foundation proper doesn't need to be changed -- |
@swift-ci smoke test |
@swift-ci please smoke test |
This is now subsumed into #23832. |
The new definition normalizes measurements with dimensional units to their base unit, ensuring that the generated hash values are consistent with the definition of ==. It also feeds the unit to the hasher, not just the numerical value.
The old
hashValue
definition was invalid, and has been removed. (I.e., replaced by a compiler-supplied implementation that works in terms ofhash(into:)
.)rdar://problem/46187171
(This PR was split out of #20685)