WAT: opCmp and opEquals woes

Daniel Gibson via Digitalmars-d digitalmars-d at puremagic.com
Fri Jul 25 03:27:27 PDT 2014


Am 25.07.2014 12:07, schrieb Jonathan M Davis:
> And once you define opEquals, you have to define
> toHash. So, what you're suggesting would force a lot more code to define
> toHash, which will likely cause far more bugs than simply requiring that

Is it actually hard to define toHash, or should it be?
What is done by default? I guess some magic hash is built over all 
members of a type (like all members are compared in opEquals).
So couldn't there be some templated function that creates the hash for 
you in the same way as it's done now, but only for the values you want 
to hash?

e.g.
hash_t createHash(T...)(T args) {
     return (do magic with args);
}


struct Foo {
     int x;
     int y;
     string str;
     int dontCare;

     bool opEquals()(auto ref const Foo o) const {
         return x == o.x  && y == o.y && str == o.str;
     }

     hash_t toHash() {
         return createHash(x, y, str);
     }
}

Cheers,
Daniel


More information about the Digitalmars-d mailing list