objects as AA keys

Ali Çehreli acehreli at yahoo.com
Tue Oct 15 14:07:34 PDT 2013


On 10/14/2013 10:44 PM, captaindet wrote:

 > Classes can be used as the KeyType. For this to work, the class
 > definition must override the following member functions of class Object:
 > hash_t toHash()
 > bool opEquals(Object)
 > int opCmp(Object)

...

 > it seems to work out of the box -
 > without overriding any member functions. in my use case, i wouldn't be
 > able to modify the class anyway.
 >
 > so my questions:
 >
 > why is it working, is it just syntactic sugar for using cast(void*)Obj
 > as key?

To be pedantic, hash values are of type size_t.

 > what is the danger of using objects as keys? when would it fail?
 >
 > as it seems to be working against language specs, will this 'feature'
 > eventually be removed?
 > (then maybe i should use cast(void*)Obj right away...)

I don't know the definitive answer but my experience was the same when I 
wrote the following chapter:

   http://ddili.org/ders/d.en/object.html

Contrary to your current need, I say "However, its default behavior is 
almost never what is desired." :)

 >
 >
 > thanks, det
 >

Ali



More information about the Digitalmars-d-learn mailing list