Can someone please explain why the following assertion fails?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Oct 31 15:10:30 PDT 2016


On 10/31/2016 12:08 PM, Gary Willoughby wrote:
> Can someone please explain why the following assertion fails?
>
> import std.stdio;
> import std.conv;
>
> void main(string[] args)
> {
>     auto x = 1;
>
>     assert(hashOf(x.to!(string)) == hashOf(x.to!(string)));
> }
>
> Thanks.

I think you need TypeInfo.getHash.

   https://dlang.org/phobos/object.html#.TypeInfo.getHash

import std.conv;

auto myHashOf(T)(auto ref T value) {
     return typeid(T).getHash(&value);
}

void main() {
     auto x = 1;
     auto s = "1";
     assert(myHashOf(x.to!string) == myHashOf(x.to!string));
     assert(myHashOf(s) == myHashOf(s));
     assert(myHashOf(s) == myHashOf(x.to!string));
}

Ali



More information about the Digitalmars-d-learn mailing list