foreach over enums?
Simen kjaeraas
simen.kjaras at gmail.com
Tue Sep 21 17:25:42 PDT 2010
%u <e at ee.com> wrote:
> == Quote from Simen kjaeraas (simen.kjaras at gmail.com)'s article
>> %u <e at ee.com> wrote:
>> > enum X { A=3, B=1, C }
>> >
>> > void main() {
>> > foreach(e;X)
>> > writefln(e.stringof," = ",e);
>> > }
>> > //X.A = 3
>> > //X.B = 1
>> > //X.C = 2
>> > or
>> > //X.B = 1
>> > //X.C = 2
>> > //X.A = 3
>> enum X { A=3, B=1, C }
>> void main( ) {
>> foreach( e; __traits(allMembers, X) ) {
>> writeln( "X.", e, " = ", mixin( "X."~e ) );
>> }
>> }
>
> D1 :'(
Oh. Without being an expert on D1 matters, I believe that is impossible.
I thought there was a struct called defineEnum in phobos1, but it
appears I am wrong. However, it should be fairly simple to create one.
Untested code, as I do not have D1 installed:
struct defineEnum( T... ) {
foreach ( i, e; T ) {
mixin( "static defineEnum " ~ e ~ " = defineEnum( i );" );
}
static int opApply( int delegate( ref bar ) dg ) {
int result = 0;
foreach ( e; T ) {
mixin( "result = dg( " ~ e ~ " ) );" );
if ( result ) {
break;
}
}
return result;
}
}
--
Simen
More information about the Digitalmars-d-learn
mailing list