Iterating through enumeration

Timon Gehr timon.gehr at gmx.ch
Sun Aug 12 07:03:44 PDT 2012


On 08/12/2012 01:54 PM, bearophile wrote:
> Era Scarecrow:
>
>> But that problem goes away if you turn it into a small array right?
>> (And pass the array to the foreach to cycle over) So throwing that
>> idea out there you get...
>>
>> foo[] array;
>>
>> foreach(m; std.traits.EnumMembers!foo)
>> array ~= m;
>>
>> foreach(m; array) {
>> //not compiler-time expansion like above
>> }
>
> Often better (but every time builds an array on the heap):
>
> foreach (m; [EnumMembers!Foo]) { // Not a static foreach
> }
>
> Bye,
> bearophile

This builds the array once at compile time:

static immutable members = [EnumMembers!Foo];
foreach(m; members){

}


More information about the Digitalmars-d-learn mailing list