Enum inheritance

Witold Baryluk baryluk at smp.if.uj.edu.pl
Wed Aug 15 10:51:02 PDT 2007


Dnia Wed, 15 Aug 2007 13:12:57 -0400
Matthias Walter <walter at mail.math.uni-magdeburg.de> napisał/a:

> For my current project, I would be happy to have some kind of enum
> inheritance, is it on the plan for future changes to D?
> 
> Syntactically, it should only be an extension to the specification of
> an enum base type via
> 
> enum Name : Base { Symbols, ... }
> 
> Why not allow other enums as a Base to import their symbols? As long
> as one does not think about multiple inheritance (or enum-interfaces
> <g>), this shouldn't be problem?! Or is it already implemented and I
> missed something?
> 
> best regards
> Matthias

import std.stdio;
import std.traits;

string Ehelper(string[] es) {
        char[] r;
        for (int i = 0; i < es.length; i++) {
                if (i) r ~= ", ";
                r ~= es[i];
        }
        return r;
}
template EE(E0a, E0b) {
        const EE = "enum E3 { "~Ehelper(__traits(allMembers, E0a)) ~ ",
" ~ Ehelper(__traits(allMembers, E0b)) ~ " }"; }

enum E1 {
        A, B, C
}

enum E2 {
        E, F, G
}

mixin(EE!(E1, E2));

void main(char[][] args) {
        writefln(E3.A);
        writefln(E3.F);
}




-- 

"EWitold Baryluk
MAIL: baryluk at smp.if.uj.edu.pl, baryluk at mpi.int.pl
JID: movax at jabber.autocom.pl



More information about the Digitalmars-d mailing list