Iterating through enumeration

Alexandr Druzhinin drug2004 at bk.ru
Sun Aug 12 04:27:42 PDT 2012


12.08.2012 18:23, Era Scarecrow пишет:
> On Sunday, 12 August 2012 at 10:45:37 UTC, Jonathan M Davis wrote:
>> Though be aware that since EnumMembers results in a TypeTuple, the
>> foreach is actually done at compile time, meaning that you'd actually
>> be getting
>>
>> bar(foo.first);
>> bar(foo.second);
>> bar(foo.fourth);
>> bar(foo.sixth);
>>
>> Normally, that doesn't matter, but if you try and do fancier stuff,
>> the fact that the foreach is executed at compile time could affect
>> what you're doing, and if want your function to be small (e.g.
>> inlining), then that would cause problems.
>
>   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
> }
It's very useful tip, thank you too!


More information about the Digitalmars-d-learn mailing list