Associative arrays in D and default comparators
Walter Bright
newshound at digitalmars.com
Fri Sep 8 12:48:42 PDT 2006
Sean Kelly wrote:
> Walter Bright wrote:
>> 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;
> }
> }
That will fail if the object gets moved.
> 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.
An opCmp has the same problem that toHash does; fix it for either and it
is fixed for both. I think you'll have to do a unique id for each in
order to store them in an AA, or else wait until the thread id is
assigned before storing it.
More information about the Digitalmars-d
mailing list