On Wednesday, 16 May 2012 at 05:54:45 UTC, Simen Kjaeraas wrote:
On Wed, 16 May 2012 06:22:15 +0200, Comrad <comrad.karlov...@googlemail.com> wrote:

Dear developers and community,
I want to draw you attention to the topic which was already discussed here:
http://forum.dlang.org/thread/9to6n8$12d6$1...@digitaldaemon.com
and here:
http://forum.dlang.org/thread/bl1n0e$1km5$1...@digitaldaemon.com

My question is: was something similar already done? are there some plans to do that?

I have implemented this functionality in user code, with some caveats:

module enumMagic;

string EnumDefAsString(T)()
        if (is(T == enum))
{
        string result = "";
        foreach (e; __traits(allMembers, T)) {
                result ~= e ~ " = T." ~ e ~ ",";
        }
        return result;
}

template ExtendEnum(T, string s)
        if (is(T == enum) &&
                is(typeof({mixin("enum a{"~s~"}");})))
{
        mixin(
                "enum ExtendEnum {"
                ~ EnumDefAsString!T()
                ~ s
                ~ "}");
}

unittest {
    enum Foo {
        a,
        b,
        c
    }
    alias ExtendEnum!( Foo, q{
        d,
        e
    }) Bar;

    Bar b;
    b = Bar.a; // Look ma, I stole this from Foo!
    b = Bar.d;
    assert( Bar.a == Foo.a ); // Can compare the two.
    //b = Foo.a; // But cannot assign from one to the other.
}

void main( ) {
}

A library implementation with the missing features is possible, but would use a struct instead of an enum, an be a rather larger piece of code than this.

This is not really useful, because one can't pass Foo into a function if it's declared to take Bar...

Reply via email to