Closed cappuccino closed 9 years ago
-hash is broken on CPArray
Why do both array’s need to return the same hash value? That doesn’t seem to be documented anywhere in the Cocoa docs.
This code seems incredibly dangerous to me. You’re essentially saying that all kinds of arrays that have nothing to do with each other could be considered equal. This will break anything that uses hash as a unique identifier (for example, KVO/Bindings, and I’m sure other code in Cappuccino).
by Ross Boucher
-hash is broken on CPArray
I spoke before I understood what I was talking about.
From the documentation for hash:
@"If two objects are equal (as determined by the isEqual: method), they must have the same hash value. This last point is particularly important if you define hash in a subclass and intend to put instances of that subclass into a collection."
Our implementation of hash does not behave this way. Perhaps we should consider changing our hash to become address, and re-implementing hash the Cocoa way?
by Ross Boucher
-hash is broken on CPArray
I’m not sure I understand you correctly, -[CPObject hash] does return __adress, I haven’t checked all the other objects, but all objects who redefine -isEqual: need their own implementation that matches the contract.
The one which I provided for CPArray - and I checked it against the two other Cocoa implementations I know, which do it the same way. (Also it makes sense: The Implementation needs to be FAST, and it needs to be the same when two objects are -isEqual:
I also checked against some examples on Cocoa and it behaves the same way.
by Martin Häcker
-hash is broken on CPArray
My point was that -hash works differently in Cappuccino than in Cocoa (something I previously had not realized).
We need access to __address as a method, which is what hash has been doing for us. To make -hash actually correct, we’ll need to add -address and update all of the cappuccino code that relies on hashes being globally unique.
by Ross Boucher
-hash is broken on CPArray
Too late to finish sentences...
The one which I provided does fit the contract and also behaves the same way that the others do.
by Martin Häcker
-hash is broken on CPArray
Yes, but it breaks parts of Cappuccino that expect the current behavior. For example, KeyValueObserving and CPWindow both rely on the value of -hash as a unique index into various data structures.
by Ross Boucher
-hash is broken on CPArray
Oh damn.
I just had a look around in the source and lots of objects like CPDictionary, CPData, CPDate and CPNull don’t overide -hash but should. (And most should probably also overide isEqual:)
Uh oh....
Yes, this is going to take some work.
by Martin Häcker
-hash is broken on CPArray
Can’t resist...
As a good person once said: In God we trust - for everything else we have UnitTests.
:)
by Martin Häcker
See also issue #66 which is related to this.
I've taken the first steps to remedy this by replacing all current instances of -hash with the new -UID method, so that when we change hash in the future, it won't break anything.
This Must be fixed.
All instances of -hash have been replaced by -UID, do we close this issue or change the -hash implementation?
So:
Okay, can you please ask about #2 on the list since you understand the issues well? Then we will know better what the ramifications of the change will be.
Assignee: boucher. Milestone: 1.0. Labels: #needs-review, Foundation, bug. What's next? This issue is pending an architectural or implementation design decision and should be discussed or voted on.
Can I have an update on the status of this issue? It seems like it has mostly been addressed and/or circumvented for now.
We basically have decided to hold off on a decision. It's still Someday.
given that a lot of code has been written since 2009, i would suggest closing this issue and documenting the current behaviour.
@aparajita, any objections?
@daboe01 Go ahead and close it, but document the problem with the current implementation compared to Cocoa.
It breaks the contract that two arrays which are -isEqual: also need to report the same -hash value.
Patch and tests included
It’s also the same way that GnuStep and Cocotron implement this - I was quite surprised how simple they implemented it.
:)
original LH ticket
This ticket has 1 attachment(s).