http://d.puremagic.com/issues/show_bug.cgi?id=5515



--- Comment #1 from bearophile_h...@eml.cc 2011-08-18 14:51:07 PDT ---
This enhancement request comes from a handy feature of the Ada language. 

It's not too much hard to implement something similar in D too, with I think an
acceptable final user syntax:


import std.traits: EnumMembers, OriginalType, Unqual;
import std.stdio: writeln;

private E[] convertEnum(E, T)(in T[] data) @safe pure nothrow
if (is(E == enum) && is(Unqual!T == OriginalType!Foo)) {
    //assert(__ctfe, "This is a compile-time function only.");

    E[T] dict;
    foreach (member; EnumMembers!E)
        dict[member] = member;

    auto result = new E[data.length];
    foreach (i, item; data)
        result[i] = dict[item];
    return result;
}

enum Foo : char { A='a', B='b', C='c' }

void show(T)(T x) { writeln(x); }

template F(string s) {
    enum F = convertEnum!Foo(s);
}

void main() {
    enum Foo[][] foos = [F!"abcabcabc",
                         F!"cbacbacba"];
    //import std.conv;
    //const Foo[] foos2 = to!(Foo[])("abcabcabc"); // not possible yet
    show(foos); // [[A, B, C, A, B, C, A, B, C],
                //  [C, B, A, C, B, A, C, B, A]]
}


Still, I think safe compile-time Enum conversion and safe run-time Enum
conversion is a feature worth folding inside to!().

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------

Reply via email to