foreach

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 13 02:15:20 PDT 2014


On Fri, 13 Jun 2014 01:00:11 +1000
Manu via Digitalmars-d <digitalmars-d at puremagic.com> 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.

I'd honestly rather see for(;;) {} removed than have foreach(; 0..n) {} added.
I don't like special cases like like these.

And I really don't think that it's a big deal to have to provide a counter
variable that you're not going to explicitly use. I can see why you'd want to
be able to skip providing the variable, but all it's saving you is typing one
character, which doesn't at all seem worth it to me.

And if the argument is that the compiler could potentially make optimizations
based on the lack of a variable, I would argue that the compiler should be
able to make those same optimizations based on the fact that the variable is
unused beyond being iterated over.

So, while I can see why you might like this, it seems like an awfully small
thing for you to try and claim it's a big improvement.

- Jonathan M Davis


More information about the Digitalmars-d mailing list