Association hash vs equality

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

Association hash vs equality

Jan Blizničenko
Hello, I've been looking at implementation of Associtation and LookupKey (its
superclass).
In LookupKey, there is LookupKey>>#= and LookupKey>>#hash both implemented
to use key (compare keys and use hash of key), but in Association,
Association>>#= compares both key and value, but hash is not changed there,
so only hash of key is used, therefore it does not correspond to
implementation of method = ... shouldn't it?

To demonstrate:
(1 -> #a) = (1 -> #b).
  is false but
(1 -> #a) hash = (1 -> #b) hash.
  is true

I also noticed similar discussion for Doplhin smalltalk
http://forum.world.st/Association-gt-gt-hash-td3368868.html where guys state
it is all right, but I still do not see why.

Jan



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Developers-f1294837.html

Reply | Threaded
Open this post in threaded view
|

Re: Association hash vs equality

Guillermo Polito
Actually, #= and #hash should be defined so:

if (a=b) then a hash = b hash
if a != b, it's not important

This is required because some collections (like sets, dictionaries) will index their elements by their hash. Then, typically the collection implementation will first ask the hash, access the index with it, and retrieve the object if it is = only. Notice that several objects can have the same hash but be equals nonetheless.

On Thu, Aug 16, 2018 at 11:36 AM Jan Blizničenko <[hidden email]> wrote:
Hello, I've been looking at implementation of Associtation and LookupKey (its
superclass).
In LookupKey, there is LookupKey>>#= and LookupKey>>#hash both implemented
to use key (compare keys and use hash of key), but in Association,
Association>>#= compares both key and value, but hash is not changed there,
so only hash of key is used, therefore it does not correspond to
implementation of method = ... shouldn't it?

To demonstrate:
(1 -> #a) = (1 -> #b).
  is false but
(1 -> #a) hash = (1 -> #b) hash.
  is true

I also noticed similar discussion for Doplhin smalltalk
http://forum.world.st/Association-gt-gt-hash-td3368868.html where guys state
it is all right, but I still do not see why.

Jan



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Developers-f1294837.html



--

   

Guille Polito

Research Engineer

Centre de Recherche en Informatique, Signal et Automatique de Lille

CRIStAL - UMR 9189

French National Center for Scientific Research - http://www.cnrs.fr


Web: http://guillep.github.io

Phone: +33 06 52 70 66 13