foreach

Mason McGill via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 12 14:27:58 PDT 2014


On Thursday, 12 June 2014 at 15:00:20 UTC, Manu via Digitalmars-d 
wrote:
> I often find myself wanting to write this:
>   foreach(; 0..n) {}
> In the case that I just want to do something n times and I don't
> actually care about the loop counter, but this doesn't compile.
>
> You can do this:
>   for(;;) {}
>
> If 'for' lets you omit any of the loop terms, surely it makes 
> sense
> that foreach would allow you to omit the first term as well?
> I see no need to declare a superfluous loop counter when it is 
> unused.

Seems like a nice idea. Especially useful when it comes to 
reading other people's code:

   foreach (i; 0..n)
   {
       some();
       long();
       list();
       of();
       tasks();
   }
   // I always double back--"wait, where did they use `i`?"


More information about the Digitalmars-d mailing list