Struct hash issues with string fields

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat May 26 12:53:07 PDT 2012


I don't understand this:

import std.stdio;

struct Symbol { string val; }

void main()
{
    int[string] hash1;
    hash1["1".idup] = 1;
    hash1["1".idup] = 2;
    writeln(hash1);  // writes "["1":2]"

    int[Symbol] hash2;
    Symbol sym1 = Symbol("1".idup);
    Symbol sym2 = Symbol("1".idup);
    hash2[sym1] = 1;
    hash2[sym2] = 1;
    writeln(hash2);  // writes "[Symbol("1"):1, Symbol("1"):1]"
}

Why are sym1 and sym2 unique keys in hash2? Because the hash
implementation checks the array pointer instead of its contents? But
then why does hash1 not have the same issue?

I can't override toHash() in a struct, so what am I supposed to do in
order to make "sym1" and "sym2" be stored into the same hash key?


More information about the Digitalmars-d-learn mailing list