Enumerating structs?

Heywood Floyd soul8o8 at gmail.com
Wed Jan 4 01:23:41 PST 2012


> Yeah, D feels like that to me too, sometimes. Anyways, for your question -
> would using the struct name be good enough? They're easy to get hold of
> and usable in switch statements.
>
> If not, how about this:
>
>
> import std.typetuple;
>
> struct TypeEnum( T... ) {
>     static pure nothrow @property
>     int value( U )( ) {
>         static assert ( staticIndexOf!( U, T ) != -1 );
>         return staticIndexOf!( U, T );
>     }
> }
>
> struct A {}
> struct B {}
>
> void main( ) {
>     alias TypeEnum!(A, B) types;
>
>     assert( types.value!A == 0 );
>     assert( types.value!B == 1 );
> }

Ha, yes, that looks quite nice!
Thanks!

/HF


More information about the Digitalmars-d-learn mailing list