foreach over enums?

Simen kjaeraas simen.kjaras at gmail.com
Thu Sep 23 16:56:49 PDT 2010


%u <e at ee.com> wrote:

>> I'm not sure what you're getting at here. In what way that you don't  
>> like
>> it is it enum-unlike?
>
> These two have distinctly different outputs ;P
>
> alias defineEnum!( "A", "B", "C" ) Bar;
> writefln( typeof(Bar.A).stringof );
>
> enum Foo { A, B, C }
> writefln( typeof(Foo.A).stringof );

They do. There are ways around that - mostly by using string mixins
on this form:

     template defineEnum( string name, T... ) {
         string defineEnum = "struct " ~ name ~ " {"
             // Stuff from other implementation goes here.
         "}"
     }

     mixin( defineEnum!( "EnumName", "A","B","C" ) );

This pattern is one of the reasons I have been lobbying for automatic
mixin templates in D2 - it should look only like this:

     defineEnum!( "EnumName", "A","B","C" )


> Won't the compiler even choke on the type size when feeding defineEnum a  
> hundred
> elements or so?

Types are limited to 64K of memory, I think. That should be enough for 16K
elements in this case. If you're thinking of the horribly long names,
I believe identifiers are hashed rather than being stored wholesale, once
they move past 16K.

-- 
Simen


More information about the Digitalmars-d-learn mailing list