On Wednesday, 27 April 2016 at 10:49:54 UTC, Nordlöw wrote:
What's the easiest way to create an `enum` using the symbol names of an `AliasSeq` as enumerator names?

/** Returns: a `string` containing the definition of an `enum` named `name` and with enumerator names given by `Es`, optionally prepended with `prefix` and
    appended with `suffix`.

    TODO Move to Phobos std.typecons
*/
string makeEnumDefinitionString(string name,
                                string prefix = `_`,
                                string suffix = ``,
                                Es...)()
    if (Es.length >= 1)
{
    typeof(return) s = `enum ` ~ name ~ ` { `;
    foreach (E; Es)
    {
        s ~= prefix ~ E.stringof ~ suffix ~ `, `;
    }
    s ~= `}`;
    return s;
}

@safe pure nothrow @nogc unittest
{
    import std.meta : AliasSeq;
    alias Types = AliasSeq!(byte, short);
    mixin(makeEnumDefinitionString!("Type", `_`, `_`, Types));
    static assert(is(Type == enum));
    static assert(Type._byte_.stringof == "_byte_");
    static assert(Type._short_.stringof == "_short_");
}

Can it be made more elegant?

Destroy!

Reply via email to