Better example:


private import std.traits, std.typecons, std.typetuple;

hash_t toHash(T)(in T t)
        if (isIntegral!(T) || isSomeChar!(T))
{ return t; }

hash_t toHash(T)(in T[] t)
{
        typeof(return) h = 0;
        foreach (item; t)
        { h = (h * 37) ^ item.toHash(); }
        return h;
}

hash_t toHash(V, K)(in V[K] t)
{
        typeof(return) h = 0;
        foreach (k, v; t)
        { h ^= k.toHash(); }
        return h;
}

hash_t toHash(T)(in T t)
        if (!isIntegral!(T) &&
            !isSomeChar!(T) &&
            !isArray!(T) &&
            !isAssociativeArray!(T))
{
        typeof(return) h = 0;
        foreach (ref a; t.tupleof)
        { h = (h * 37) ^ a.toHash(); }
        return h;
}

void main()
{
        import std.stdio;
        writeln(tuple("hi", [[1: 2].keys.dup], [1: 2, 8: 4]).toHash());
writeln(tuple("hi", [[1: 2].keys.dup], [1: 2, 8: 4]).toHash()); // same thing as above
}





We really _need_ something like this in Phobos IMHO.

Reply via email to