Design of reflective enums

renoX renosky at free.fr
Mon Feb 19 04:14:27 PST 2007


Hello,
Kevin Bealer has started an implementation (two implementations in fact!) of reflective enums, and I wanted to discuss the use case and the design of this feature to make it as useful as possible.
Reflective enums have two useful properties that don't have normal enums:
- their label is printable
- it's possible to do a foreach on all the possible values.

In my opinion, for the reflective enums, the following constraint should apply:
- they should 'look like' normal enums as much as possible.
- then the printable label feature should be as simple to use as possible: this feature will  probably be used much more often than the foreach feature.

For the API, the functions needed are:
a- Reflective Enum(REnum) list definition.
b. REnum variable definition.
c. Reference to an REnum label.
d. Print the label corresponding to the value of an REnum variable.
e. Iterate over all possible value in an REnum list.
f. Iterate over all possible labels in an REnum list (I've added this one by symmetry).

(a) Takes a name and either define (1) an enum of this name plus some functions to print enum labels and iterate over it, or alternatively as in the original code it can return a 'thing' (struct for example as it in the initial implementation) (2) which contain the enum and functions.
I'm not sure which is best.

(e) and (f) reminds me of associate arrays, maybe a function to generate an associative array from the enum list would be enough..

I'm trying to create functions to (e) and (f), but I have trouble to make my code work currently: debugging template code is not very easy..

renoX




More information about the Digitalmars-d mailing list