Associative arrays, opCmp, opEquals and toHash in 2.066.0-b4

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Mon Jul 21 07:58:35 PDT 2014


Daniel Gibson:

> That would be too much magic => special cases one has to know 
> about.

What special cases? It works for POD structs. If you want a 
different hashing behavior (example: you want to ignore some 
fields), you define the two methods.


struct Foo {
     double x;
     int y;
     string s;
}

void main() {
     import std.stdio;

     bool[Foo] aa;
     aa[Foo(1.0, 2, "hello".idup)] = true;
     aa[Foo(1.0, 1, "hello".idup)] = false;
     aa[Foo(1.0, 2, "hello".idup)] = false;
     aa.writeln;
}


Outputs:

[Foo(1, 1, "hello"):false, Foo(1, 2, "hello"):false]

This was a very old large bug of D. Now it's fixed. Rejoice.

Bye,
bearophile


More information about the Digitalmars-d mailing list