"toString(enum.value)" How?

Jason House jason.james.house at gmail.com
Sun Jul 8 12:18:03 PDT 2007

The topic of converting enums to strings has come up before.

IIRC, the problem is that enums tend to be used for one of two purposes: 
a collection of fixed constants, and bit fields/masks.  While the former 
may be easy to do, the latter presents a problem...  Especially when the 
values of different fields overlap.

Ingo Oeser wrote:
> 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,
> }
> 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