Thread overview
enum <-> string
Mar 11, 2005
Andrew Fedoniouk
Mar 11, 2005
Andrew Fedoniouk
Mar 14, 2005
Charlie Patterson
March 11, 2005
Gentlemen, let me summarize this enum/string discussion.

Task/goals:

Handy and natural (a.k.a. aesthetically acceptable) support of

1)  enum -> string  (a.k.a. toString(in E e))
2)  string -> enum  (a.k.a. bool fromString(in str, out E e))

in D language.

Let's assume that these task are pretty frequent/popular
in programming practice.
Let's also assume that number of items (enum values) in
such stringized enums is not too much more than 30-40 items.

Typical task:
color name <-> color value definition and support
(16 standard colors)

So all this is about relatively small sets of values.
For bigger sets there are specialized and highly effective
solutions : perfect hashes (gperf.exe), compiled ternary trees
and the like.

Requirements:

a) Desired "enum with string" syntax notation should allow
to write symbolic (D namespace) name of enum constant
with optional value and ideally "on the same line" some
textual (UI) description or enum text value.

b) Implementation of lookups
enum -> string and string -> enum should not use
too much extra code (increase size of executable)
but it should be reasonable fast - lookup
computational complexity ideally should be between
O(log(n)) - O(n).

c) Very desirable - to design notation/solution allowing
to "plug-in" different lookup mechanisms on the same
notation base.

d) Solution may not support internationalization (dynamic),
but it is desiribale.


Let's start discussion from these points.

Imho make sense to review Java 1.5 enums at least.

Andrew.
http://terrainformatica.com


March 11, 2005
And yet... this task has nothing common with reflection. These two has completely different goals.

enum <-> string task is close to implemetation
of static associative array initializers.


March 14, 2005
"Andrew Fedoniouk" <news@terrainformatica.com> wrote in message news:d0rgbr$1603$1@digitaldaemon.com...
> And yet... this task has nothing common with reflection. These two has completely different goals.
>
> enum <-> string task is close to implemetation
> of static associative array initializers.

Maybe the point is that, instead of specifying strings to go with enums, you simply get the enum label.  This would be handy for programming and very simple:

enum Colors { Red, Green, Blue };
enum ProcTypes { PT_Threaded, PT_Compartment };

Colors C = Green;
ProcTypes P = PT_Threaded;

println( "%s %s", C, P );

produced "Red PT_Threaded".

It's not as verbose or "user-friendly" as supplying your own strings, but it is better than printing "1 1".