"toString(enum.value)" How?
Jarrett Billingsley
kb3ctd2 at yahoo.com
Sun Jul 8 19:23:22 PDT 2007
"Ingo Oeser" <ioe-news at rameria.de> wrote in message
news:f6r5mi$2q7n$1 at digitalmars.com...
>
> How is that kind of problem solved in D WITHOUT
> keeping two tables?
>
Unfortunately the language also doesn't provide any way of querying
information about an enum other than its min, max, and default values.. it
would be nice, given a named enum, to get a .tupleof the valid values, where
each item in the tuple would be an alias to one of the valid enum values.
Furthermore, EnumValue.stringof would have to give a string representation
of the actual enum value, rather than a string of the type as it does now (I
think .stringof is still not 100% finished anyway..). With those two
abilities, generating some kind of lookup table at compile-time would be
very easy indeed.
But of course as Jason pointed out, one issue is with flag enums. C# kind
of solves this by allowing you to put a "Flags()" attribute or so on the
enum, which gives the runtime a hint as to how to stringize a given enum
value (it comes out as something like "Blah.A, Blah.B, Blah.C" if you have
multiple values or'ed together). I'd even go so far as to say that the use
of an enum as a simple list of constants and the use of an enum as a list of
flags is divergent enough to warrant a new language construct -- i.e. "flags
Blah { A, B, C }". You wouldn't have to manually specify the values as you
have to with flag enums, and it makes sense to Or together multiple flag
values; it usually doesn't make sense to Or together multiple values of a
"constants" enum.
More information about the Digitalmars-d-learn
mailing list