Last update March 7, 2012

Doc Comments /
Enum



Enums    

Table of contents of this page
Enums   
Examples   
Explicit Initialization   
Comments   
Virtual Enumeration   
Extendable Enums   
init property for enums   
Links   

Examples    

Explicit Initialization    

Quick basic example of explicit initialization of enums perhaps assumed to be 'obvious' by the documentation:

enum ORIENTATION : byte { HORIZ, VERT }
        ...
// Wrong way:
ORIENTATION foo = 0;        // Error, NOT 'ORIENTATION' enum type.
ORIENTATION bar = HORIZ;    // Error, 'HORIZ' out of 'ORIENTATION' scope -- use full qualification.

// Correct way:
ORIENTATION foobar = ORIENTATION.HORIZ;     // OK. (Unlike C++, full qualification required).
ORIENTATION barfoo;                         // Defaults to 'ORIENTATION' first member (HORIZ = 0).

Note the example above requires full qualification. If you find this cumbersome, you can ease the pain with an alias.

enum ORIENTATION : byte { HORIZ, VERT }
alias ORIENTATION.VERT  VERT;
alias ORIENTATION.HORIZ HORIZ;
        ...
ORIENTATION foofoo = VERT;
ORIENTATION barbar = HORIZ;

- JoseSantos -

Comments    

Add your comments here.

Virtual Enumeration    

something I find missing from c++ is "virtual enumeration". there are cases when a subclass needs to redefine the items of an enum field. this can be solved by using integers, but debugging now becomes more difficult since the values are no longer presented by their enumeration members, even though the debugger knows the type of class it is dealing with. the c++ usage would have been something like this:

class A
{
    virtual enum { a1, a2, a3 }	x;
};

class B : public A
{
    enum { b1, b2, b3, b4 }  x;
};

technically, this means the type of x had changed, but maybe in D it would be legal.

Extendable Enums    

I would like to have extendable enums:

enum BasicColor { 
  red, green, blue
};

enum Color : BasicColor {
  pink, grey, yellow, magenta
};
What may make things difficult, is the fact that it reverses the inheritance principle know from objects because each BasicColor? is definetly a Color but not the other way around.

init property for enums    

The first enum value (.init) often happens to be the minimum, but this is not true when they are assigned out of order. Looking below, .min = 0 but .init = 6 (perhaps there should be an alternate way to specify the initial value as to avoid reordering the values and risking overlap?).

enum RenderMode {
    Opaque=6, Transparent=0, ColorKeyed, Additive, Subtractive, ...
}

Links    


FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: March 7, 2012 20:24 (diff))