an enum inside another enum

bearophile bearophileHUGS at lycos.com
Thu Jul 26 06:07:56 PDT 2012


maarten van damme:

> enum first : string{
>  a="a",
>  b="b"
> }
>
> enum second : string{
>  a=first.a,
>  b=first.b,
>  c="c"
> }
>
> Is there a way to make this cleaner?

By convention in D enum names start with an upper case.

I have tried this, partially derived by Simen Kjaeraas code (with 
T.stringof), but isn't D supporting mixins inside enums?


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

enum First : string {
     a = "a",
     b = "b"
}

enum Second : string {
     mixin(EnumInh!First),
     c = "c"
}

void main() {}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list