"toString(enum.value)" How?

Ingo Oeser ioe-news at rameria.de
Sun Jul 8 10:11:50 PDT 2007


Hi,

while trying to code/port some stuff heavily relying on enums,
I asked myself, whether there is a nice and elegant way
to actually get the name of an enum value as string.

enum Funny {
        UNKNOWN,
        MAKES_ME_SMILE,
        LAUGHING_OUT_LOUD,
        ROLLING_ON_THE_FLOOR_LAUGHING
}

void main()
{
        Funny joke = Funny.LAUGHING_OUT_LOUD;
        wrifeln("%s", toString(joke));
}

should print "Funny.LAUGHING_OUT_LOUD". I could also
live with "LAUGHING_OUT_LOUD". But keeping these two tables
(string[] and enum) in sync requires some kind of preprocessor 
and D has none.

How is that kind of problem solved in D WITHOUT 
keeping two tables?

Many thanks and Best Regards

Ingo Oeser



More information about the Digitalmars-d-learn mailing list