Can someone please explain why the following assertion fails?
Gary Willoughby via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Nov 1 01:45:16 PDT 2016
On Monday, 31 October 2016 at 22:10:30 UTC, Ali Çehreli wrote:
> 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
Thanks but `TypeInfo.getHash` is not @nogc. Looks like I'll have
to roll my own.
More information about the Digitalmars-d-learn
mailing list