Feature request: extending comma operator's functionality
H. S. Teoh
hsteoh at quickfur.ath.cx
Fri Oct 5 09:58:39 PDT 2012
On Fri, Oct 05, 2012 at 05:23:40PM +0200, Don Clugston wrote:
[...]
> My feeling is that do{}while() is a fairly useless concept, and
> this is part of the reason.
> In my experience genuine do-while loops are extremely rare, and it
> only takes a slight change to the loop to force a different
> structure to be used.
> Conditional loops which don't follow the while(){...} pattern
> normally follow the loop-and-a-half pattern, also known as
> begin-while-repeat (I think that's the name Knuth used). I'll call
> it 'super do':
>
> super do {
> foo();
> while(cond);
> bar();
> }
>
> which in D is better modelled by:
>
> for (;;)
> {
> foo();
> if (!cond) break;
> bar();
> }
This isn't "super do", it's just "loop", the way nature intended. ;-)
I've always been an advocate of this construct:
loop {
// initial part of loop body
} while(cond) { // exit point
// trailing part of loop body
}
To some extent, D (and C/C++)'s for-loops exhibit a similar structure:
for (X; Y; Z) {}
The trailing part of the loop body corresponds with Z; the condition
corresponds with Y.
To avoid the introduction of a new keyword, we may fuse the do-loop and
the while-loop together:
do {
...
} while (cond) {
...
}
The current do-loop is simply a special case of this construct where the
second {...} is replaced with a ;, and the while-loop is a special case
of this construct where the initial part of the loop is elided.
I argue that this generalized construct is much more useful than the
do-loop or while-loop individually, plus it doesn't break any existing
code.
T
--
Без труда не выловишь и рыбку из пруда.
More information about the Digitalmars-d
mailing list