Struct hash issues with string fields

Jonathan M Davis jmdavisProg at gmx.com
Sat May 26 15:10:51 PDT 2012


On Sunday, May 27, 2012 00:08:01 Andrej Mitrovic wrote:
> 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:

Yeah. I don't know what the deal is. There's definitely at least one bug here, 
if not several.

> 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.

toHash. Sorry. All the others are op, so it's easy to forget.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list