Associative arrays in D and default comparators

Sean Kelly sean at f4.ca
Fri Sep 8 08:21:12 PDT 2006


Walter Bright wrote:
> Sean Kelly wrote:
>> Walter Bright wrote:
>>> Can you give an example of a class that could not have a meaningful 
>>> opCmp implementation that one would want to put into an AA?
>>
>> The one I've been wrestling with is a Thread class, as it has no data 
>> that is static and unique for the life of the object.  The thread id 
>> is only guaranteed to be unique while the thread is active, and the id 
>> isn't even assigned until Thread.start is called.  Threads simply have 
>> no features which inherently support ordering, but it makes sense to 
>> compare two Thread objects for equality simply by comparing object 
>> addresses.
> 
> How do you do a hash for it if there's no constant data?

Using address, currently :-p  But that obviously has to change.  What 
might make the most sense from a hash perspective would be something 
like this:

class C {
     hash_t hash;
     hash_t toHash() {
         if (hash != hash.init)
             return hash;
         hash = cast(hash_t) this;
         return hash;
     }
}

But that leaves opCmp to wrestle with.  An "object id" would be a 
reasonable universal solution, but I don't want to pay for the 
synchronized op on construction.  I suppose I'm simply having a hard 
time getting over the idea that an object's address is is not a unique 
identifier in GCed languages.


Sean



More information about the Digitalmars-d-bugs mailing list