foreach

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 13 10:46:17 PDT 2014


On Fri, 13 Jun 2014 14:25:19 +0000
monarch_dodra via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> On Friday, 13 June 2014 at 09:29:25 UTC, Jonathan M Davis via
> Digitalmars-d wrote:
> > I'd honestly rather see for(;;) {} removed than have foreach(;
> > 0..n) {} added.
>
> Just out of curiosity, what is it you don't like about "for(;;)".
>
> For what it's worth, I like using "for(;;)" a lot, because quite
> often, I find myself writing loops, but I don't know ahead of
> time *what* my conditions will be, nor *where* I'll place them.
> "for(;;)" is the first "rock" in my algorithm, which I fill and
> modify as I need while writing my code.
>
> I tend to avoid "while(xxx)", for exactly this reason too: They
> tend to just end up having to be changed into a "for", so might
> as well just write "for(;xxx;)"

for(;;) is a special case with no real benefit IMHO. It's a loop whose
condition is implicitly true rather than actually having a condition in it.
IMHO, it should required to be at least for(;1;) or for(;true;), since those
don't require a special case. And if that's what you're doing, you might as
well just use while(1), since there's no point in using for over while if
you're not doing anything in the other two parts of the for loop. And
personally, I wouldn't consider the possibility of needing to change
while(xxx) to for(yyy; xxx; zzz) to be a good enough reason to avoid while,
but I suppose that that's a stylistic argument. Regardless, all that having
for(;;) in the language buys you is saving you from typing 1 or true between
the semicolons, and it adds a special case to the language. So, IMHO, having
it was a bad design decision, but it's probably baggage from C/C++ that got
carried over into D (I don't know if it's legal in C/C++ or not, since I'd
never do it).

- Jonathan M Davis


More information about the Digitalmars-d mailing list