Iterating through enumeration

Era Scarecrow rtcvb32 at yahoo.com
Sun Aug 12 04:23:37 PDT 2012


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
}


More information about the Digitalmars-d-learn mailing list