Design of reflective enums

Kevin Bealer kevinbealer at gmail.com
Tue Feb 20 20:25:28 PST 2007


renoX wrote:
> I've ran into trouble trying to implement the associative array: 
> apparently it's not possible to build an associative array at compile time.
> 
> Added to that the fact that overloading of functions doesn't work 
> correctly on enums, the implementation of good reflective enum starts to 
> look really difficult..
> 
> So it's a choice between
> -using your initial implementation, which is not typesafe..
> -restricting reflective enums to enums without '=' ie with values going 
> 0,1,2..
> -waiting that Walter fix the current limitations on associative arrays 
> or the bug(?) on function overloading with enums (overloading with a 
> typedef has the same issue, so maybe it's me who doesn't understand the 
> overload behaviour?)..
> 
> 
> :-(
> renoX

I want to make clear -- efficiency is important to me, at least if this 
is to be a replacement for enum, efficiency should be as much as is 
attainable, which should be a lot if it happens at compile time.

Just to clarify my previous idea, when I say 'CTAA', I don't mean 
building a real AA at compile time.  I mean building something that 
works like an AA as far as lookups, but the contents are readonly and 
added to the container at compile time.  My idea is to get maximal 
efficiency of the type that most likely can't be had with a real AA.

The ultimate efficiency in this case would be to have *lookup* happen at 
compile time for users of the class, though this can only happen in 
those cases where the search term is constant at compile time.  In other 
cases, something like a switch would be my preference.

Of course for the case of a reflective enum, looking up the label is 
probably not too common so it could be done some other way, but I'm 
thinking the CTAA should be as fast as possible.  What I'm imagining is 
something like this:

CTAA!("dog, bear, cat, snake, whiskey, soda") foo;

const char[] x = foo.find!("dog")(); // A
const char[] x = "bear";             // B

My hope is for a design where statement A is transformed into statement 
B at compile time.

Kevin



More information about the Digitalmars-d mailing list