Association>>hash

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Association>>hash

Bill Schwab-2
Blair,

I suspect the answer will be that it's intentional, either for historical or
other reasons, but please take a look at Association>>hash vs. #=.  The
equality uses both key and value (which seems correct), but #hash uses only
the key.

I noticed the apparent discrepancy after having a Dictionary, with
Associations as keys, appear to return an incorrect element.  The code
involved is more than a little threaded, and I could have been seeing a
dirty read instead of a bug in Dictionary.  I tried without sucess to
reproduce the problem in a simpler case.  Then I changed the design,
replacing the associations with slightly smarter objects that do some very
useful things and also compare and hash to match the problem.

Have a good one,

Bill

--
Wilhelm K. Schwab, Ph.D.
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Association>>hash

Ian Bartholomew-18
Bill,

> I suspect the answer will be that it's intentional, either for
> historical or other reasons, but please take a look at
> Association>>hash vs. #=.  The equality uses both key and value
> (which seems correct), but #hash uses only the key.

That should be OK.

If #= answers true then that means that the Associations keys must match
(it's part of the test).  If the keys match then using their value to
calculate #hash is legitimate.  a = b means that a hash = b hash

If #= answers false then it doesn't matter what the #hash method
answers, or how it is calculated.  a ~= b does not have to mean
that a hash ~= b hash

--
Ian


Reply | Threaded
Open this post in threaded view
|

Re: Association>>hash

Blair McGlashan
"Ian Bartholomew" <[hidden email]> wrote in message
news:bmkdbi$notrj$[hidden email]...
> Bill,
>
> > I suspect the answer will be that it's intentional, either for
> > historical or other reasons, but please take a look at
> > Association>>hash vs. #=.  The equality uses both key and value
> > (which seems correct), but #hash uses only the key.
>
> That should be OK.
> ...

Ian is right. The rule is only that two equal objects must have the same
hash, so one could implement it so that every object has the same hash. This
wouldn't lead to good hashed collection performance, but it would be
"correct".

The implementation of #hash (to use only the key) and #= (to compare both
key and value) in Association follows the standard Smalltalk-80 behaviour.

Regards

Blair