www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Extending enum

Since we're empowering the keyword "enum" to make compile time
constants, how about beefing it up a bit in its original use?

    enum E : real { PI=3.14, E=2.71 };
    enum F : string { HELLO="hello", WORLD="world" }

Then you could write functions like

    string toFrench(F s) {/*...*/}
    string toGerman(F s) {/*...*/}
    string toSpanish(F s) {/*...*/}

knowing that s could only be one of "hello" or "world". It occurs to
me that that could be useful in internationalization, where you have a
fixed number of strings which have to be translated, and each is
represented by some sort of identifier. (...and even without a real
world usage example, it's still an obvious extension).
Dec 09 2007