Now that we have `static foreach` I just realized we can start making recursive implementations of traits such as

template allSame(V...)
    if (isExpressions!V)
{
    static if (V.length <= 1)
    {
        enum allSame = true;
    }
    else static if (V.length & 1) // odd count
    {
        enum allSame = (V[0] == V[$ - 1] &&
                        V[0 .. $/2] == V[$/2 .. $-1]
                        allSame!(V[0 .. $/2]));
    }
    else                        // event count
    {
        enum allSame = (V[0 .. $/2] == V[$/2 .. $]
                        allSame!(V[0 .. $/2]));
    }
}

iterative as

template allSameIterative(V...)
{
    static if (V.length <= 1)
    {
        enum allSameIterative = true;
    }
    else
    {
        static foreach (Vi; V[1 .. $])
        {
            static if (V[0] != Vi)
            {
                enum allSameIterative = false;
            }
        }
static if (!is(typeof(allSameIterative) == bool)) // if not yet defined
        {
            enum allSameIterative = true;
        }
    }
}

Is this preferred, typically in terms of compile-time memory usage and speed?

Reply via email to