foreach over enums?

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


%u <e at ee.com> wrote:

> First question, shouldn't the first foreach be replaced by a simple for  
> loop:
> for(int i = 0; i<num; i++ ) {

It certainly could, and that would explain the presence of 'num', but
it's not necessary, and should make no difference in the generated
executable.


> If I understand it correctly the foreach aggregates are actually  
> string-tuple
> literals.

Yes.


> Then couldn't the second one be CT translated to char[][]?
> That way the stringOf would be a simple array index.

Indeed it could, though I will leave its implementation as an
exercise for the reader :p. I also thought up a Duff's
device-inspired contraption that works:

     string toString( ) {
         switch ( value ) {
             foreach ( i, e; T ) {
                 case i:
                     return T[i];
             }
         }
     }

This should be as fast as the char[][].


> And lastly, I probably need to make the whole struct a string mixin if I  
> want it's
> type to be like an enum.. don't I? :'(

I'm not sure what you're getting at here. In what way that you don't like
it is it enum-unlike?

-- 
Simen


More information about the Digitalmars-d-learn mailing list