foreach

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Sat Jun 14 14:23:36 PDT 2014


On Sat, 14 Jun 2014 10:20:51 +0000
via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> On Friday, 13 June 2014 at 21:41:43 UTC, Jonathan M Davis via
> Digitalmars-d wrote:
> > On Fri, 13 Jun 2014 11:03:14 -0700
> > "H. S. Teoh via Digitalmars-d" <digitalmars-d at puremagic.com>
> > wrote:
> >> I disagree, it's not a special case. It's simply a logical
> >> consequence
> >> of each part of the for-loop being optional. Prohibiting
> >> for(;;) would
> >> *be* a special case, because then you're saying that each
> >> component of
> >> the for-loop is optional, *except* when all of them are
> >> omitted.
> >>
> >> (Not to mention, for(;1;) is truly an eyesore, far worse than
> >> for(;;).)
> >
> > It's a special case in that the middle portion is supposed to
> > be the condition
> > that the loop use to determine whether it can continue, and
> > omitting it means
> > that it has to add the true itself, whereas with the other two
> > pieces it makes
> > perfect sense that they'd be optional, since they're not
> > required to determine
> > whether the loop needs to terminate. They're just handy helpers.
>
> But this special treatment of the second operand is the same in
> all forms of the for loop:
>
>      for(int i = 0; ; ++i)    // endless loop
>
> Therefore, the fact that `for(;;)` is also an infinite loop is
> not an exception.

Regardless of whether there's anything in the other two parts of the
loop, it's still ommitting the condition, which IMHO is a special case
and incredibly bizarre. Rather than the compiler having a condition to
check, suddenly it has to know that it needs to essentially insert true
for the condition, because the condition isn't there. This is in
contrast to the other two parts of the loop which are handy places to
put statements, and having a line with no statement isn't special at
all.

It's the lack of a condition that I object to. IMHO it's a fundamental
violation of how loops work. Unfortunately, however, we're stuck with
it. Even if everyone in this thread were in agreement on this (and
obviously we're not), it's apparently a long-standing construct from C,
so I doubt that Walter would be interested in removing it.

- Jonathan M Davis


More information about the Digitalmars-d mailing list