Getting the hash of any value easily?
Daniel Gibson via Digitalmars-d
digitalmars-d at puremagic.com
Wed Jul 30 18:22:23 PDT 2014
Am 31.07.2014 03:07, schrieb Andrei Alexandrescu:
> A coworker asked about the idiomatic way to get the hash of a string in
> D, and somewhat surprisingly the best answer I could find is: "to get
> the hash of an lvalue x, call typeof(x).getHash(&x)."
>
> That's admittedly quite clunky and indirect. Is it worth it to simplify
> that by means of a template in object.di?
>
>
> Andrei
I proposed something like the following in another thread and think it
would be really useful (to implement toHash() amongst other things):
hash_t createHash(T...)(T args)
{
// return a hash over all arguments
}
In that thread (I think it was "WAT: opCmp and opEquals woes") it was
implied that currently the compiler generates a toHash() automagically
by hashing all members (of a struct or class) - analogous to opEquals
that just compares all members.
I can't find any documentation on that behavior.. but if it indeed
exists there must already be a sensible hash algorithm for multiple
values of different types which could just be reused here.
Of course hashing a single value is just a special case of this.
Cheers,
Daniel
More information about the Digitalmars-d
mailing list