Thread overview
Enum inheritance
Aug 15, 2007
Matthias Walter
Aug 15, 2007
Witold Baryluk
Aug 15, 2007
Witold Baryluk
Aug 15, 2007
Matthias Walter
Aug 15, 2007
Witold Baryluk
Aug 15, 2007
Matthias Walter
Aug 15, 2007
Witold Baryluk
Aug 15, 2007
Robert Fraser
Aug 15, 2007
Matthias Walter
Aug 15, 2007
BCS
August 15, 2007
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
August 15, 2007
Dnia Wed, 15 Aug 2007 13:12:57 -0400
Matthias Walter <walter@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@smp.if.uj.edu.pl, baryluk@mpi.int.pl
JID: movax@jabber.autocom.pl
August 15, 2007

Or better:

template EE(E0a, E0b) {
        const EE = "{ "~Ehelper(__traits(allMembers, E0a)) ~ ", " ~
Ehelper(__traits(allMembers, E0b)) ~ " }";
}


mixin("enum E3 "~EE!(E1, E2));



-- 
Witold Baryluk
MAIL: baryluk@smp.if.uj.edu.pl, baryluk@mpi.int.pl
JID: movax@jabber.autocom.pl
August 15, 2007
Witold Baryluk Wrote:

> Dnia Wed, 15 Aug 2007 13:12:57 -0400
> Matthias Walter <walter@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);
> }

Can I do the following conversion with your code?

E2 bla = E2.E;
E3 foo = bla;

best regards
Matthias
August 15, 2007
Dnia Wed, 15 Aug 2007 14:13:09 -0400
Matthias Walter <walter@mail.math.uni-magdeburg.de> napisał/a:

> 
> Can I do the following conversion with your code?
> 
> E2 bla = E2.E;
> E3 foo = bla;
> 
no.
you can do:
E1 bla = E1.A;
E3 foo = bla;

Enums in D are really integers, and it will not be so simple to do what you want. I don't know if opCast can be done with enums.

Mayby such trick:

enum E1 {
    A, B, C
}

enum E2 {
    E = E1.max+1, F, G
}


then it will work.


best regards

-- 
Witold Baryluk
MAIL: baryluk@smp.if.uj.edu.pl, baryluk@mpi.int.pl
JID: movax@jabber.autocom.pl
August 15, 2007
Matthias Walter Wrote:

> 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?

I made a number of enum extensions that renders each enum as a mixinable template that creates a struct. They allow pretty much everything people have been asking for in enums short of Java-style "enum classes." They have string printing, enum extension, iteration through enum values, ... Yeah, that's about it. And they're still only represented by one int, so no performance penalties.

I can post them if you want... Maybe I should put them in Scrapple.
August 15, 2007
Witold Baryluk Wrote:

> Dnia Wed, 15 Aug 2007 14:13:09 -0400
> Matthias Walter <walter@mail.math.uni-magdeburg.de> napisa³/a:
> 
> > 
> > Can I do the following conversion with your code?
> > 
> > E2 bla = E2.E;
> > E3 foo = bla;
> > 
> no.
> you can do:
> E1 bla = E1.A;
> E3 foo = bla;
> 
> Enums in D are really integers, and it will not be so simple to do what you want. I don't know if opCast can be done with enums.
> 
> Mayby such trick:
> 
> enum E1 {
>     A, B, C
> }
> 
> enum E2 {
>     E = E1.max+1, F, G
> }
> 
> 
> then it will work.

But the compiler will complain that E1 and E2 are different types, so semantically, your updated version will work then, but you would still need a cast!

Thanks for your working version, but you see that it's not very easy to create (you need mixins) and use (need casts) the enums this way. I believe that some kind of enum inheritance is worth to be included in the language. Any other opinions or arguments, why this is not a good idea at all?

best regards
Matthias
August 15, 2007
Robert Fraser Wrote:

> Matthias Walter Wrote:
> 
> > 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?
> 
> I made a number of enum extensions that renders each enum as a mixinable template that creates a struct. They allow pretty much everything people have been asking for in enums short of Java-style "enum classes." They have string printing, enum extension, iteration through enum values, ... Yeah, that's about it. And they're still only represented by one int, so no performance penalties.
> 
> I can post them if you want... Maybe I should put them in Scrapple.

I'm not in a hurry, so putting them in scrapple would be a good idea.

thanks
Matthias
August 15, 2007
Dnia Wed, 15 Aug 2007 14:28:03 -0400
Matthias Walter <walter@mail.math.uni-magdeburg.de> napisał/a:

> Witold Baryluk Wrote:
> 
> > Dnia Wed, 15 Aug 2007 14:13:09 -0400
> > Matthias Walter <walter@mail.math.uni-magdeburg.de> napisał/a:
> > 
> > > 
> > > Can I do the following conversion with your code?
> > > 
> > > E2 bla = E2.E;
> > > E3 foo = bla;
> > > 
> > no.
> > you can do:
> > E1 bla = E1.A;
> > E3 foo = bla;
> > 
> > Enums in D are really integers, and it will not be so simple to do what you want. I don't know if opCast can be done with enums.
> > 
> > Mayby such trick:
> > 
> > enum E1 {
> >     A, B, C
> > }
> > 
> > enum E2 {
> >     E = E1.max+1, F, G
> > }
> > 
> > 
> > then it will work.
> 
> But the compiler will complain that E1 and E2 are different types, so semantically, your updated version will work then, but you would still need a cast!
> 
> Thanks for your working version, but you see that it's not very easy to create (you need mixins) and use (need casts) the enums this way. I believe that some kind of enum inheritance is worth to be included in the language. Any other opinions or arguments, why this is not a good idea at all?

so try this:

// Copyright: Public Domain
// Author: Witold Baryluk <baryluk@smp.if.uj.edu.pl>
// Date: 2007-08-15
import std.stdio;
import std.traits;

string tostr(int i) {
        if (i < 10) {
                char[] r;
                r ~= cast(char)('0'+i);
                return r;
        } else {
                return tostr(i/10) ~ cast(char)('0'+i%10);
        }
}

typedef int ET;

string Ecreate(string name, string[] es, int start = 1, string[] org =
[]) { char[] r;
        r ~= "struct "~name~" {\n";
        r ~= "  ET x;\n";
//      r ~= "  ET opCast() { return x;}\n";
        r ~= "  static "~name~" opCall(ET _x) { "~name~" temp; temp.x =
_x; return temp; }\n"; for (int i = 0; i < org.length; i++) {
                r ~= "  static "~name~" opCall("~org[i]~" _x)
{ "~name~" temp; temp.x = _x.x; return temp; }\n"; }
        for (int i = 0; i < es.length; i++) {
                r ~= "  static "~name~" "~es[i]~" = "~name~"("~tostr(i
+start)~");\n"; }
        r ~= "  static const min = "~tostr(start) ~ ";\n";
        r ~= "  static const max = "~tostr(start+es.length-1) ~ ";\n";

        r ~= "  const __all = [cast(char[])";
        for (int i = 0; i < es.length; i++) {
                if (i) r ~= ", ";
                r ~= "\""~es[i]~"\"";
        }
        r ~= "];\n";
        r ~= "  string toString() {\n";
        r ~= "          return __all[x-min];\n";
        r ~= "  }\n";

        r ~= "}\n";
        return r;
}

mixin(Ecreate("E1", ["A", "B", "C"]));
mixin(Ecreate("E2", ["D", "E", "F"], E1.max+1));

template Emerge(Ea, Eb) {
        const Emerge = Ecreate("E3", cast(string[])(Ea.__all ~
Eb.__all), Ea.min, [cast(string)Ea.stringof, Eb.stringof]); }

mixin(Emerge!(E1, E2));

void main(char[][] args) {
        writefln("E1.A=",E1.A);
        writefln("E2.F=",E2.F);
        writefln("E3.A=",E3.A);
        writefln("E3.F=",E3.F);

        E1 b1 = E1.A;
        E3 c1 = b1;
        writefln("c1=", c1);

        E2 b2 = E2.F;
        E3 c2 = b2;
        writefln("c2=", c2);

        writefln("sizeof=",c1.sizeof);
}



// 20 minutes of coding.


-- 
Witold Baryluk, aleph0
MAIL: baryluk@smp.if.uj.edu.pl
JID: movax@jabber.autocom.pl
August 15, 2007
Matthias Walter wrote:
> Robert Fraser Wrote:
> 
> 
>>Matthias Walter Wrote:
>>
>>
>>>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?
>>
>>I made a number of enum extensions that renders each enum as a mixinable template that creates a struct. They allow pretty much everything people have been asking for in enums short of Java-style "enum classes." They have string printing, enum extension, iteration through enum values, ... Yeah, that's about it. And they're still only represented by one int, so no performance penalties.
>>
>>I can post them if you want... Maybe I should put them in Scrapple.
> 
> 
> I'm not in a hurry, so putting them in scrapple would be a good idea.
> 
> thanks
> Matthias

go ahead! that's what scrapple is for. (If you need access email me shro882 at vandals dot uidaho dot edu)