Struct hash issues with string fields

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat May 26 15:08:01 PDT 2012


On 5/27/12, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> Why can't you have a toHash in your struct?

I mean it doesn't seem to make any difference:

import std.stdio;

struct Foo
{
    string x;
    size_t toHash() { return 1; }
}

void main()
{
    int[Foo] hash;
    Foo foo1 = Foo("a".idup);
    Foo foo2 = Foo("a".idup);
    hash[foo1] = 1;
    hash[foo2] = 2;

    writeln(hash);  // "[Foo("a"):2, Foo("a"):1]"
}

> If anything, I find it disturbing
> that the code compiles _without_ a definition for opHash in the struct.

Wait, is it toHash or opHash? I can't find any documentation of
opHash, all I see is a mention of toHash in object.d.


More information about the Digitalmars-d-learn mailing list