On 05/10/12 15:35, monarch_dodra wrote:
On Friday, 5 October 2012 at 00:22:04 UTC, Jonathan M Davis wrote:
On Friday, October 05, 2012 02:08:14 bearophile wrote:
[SNIP]
Regarding definition of variables in D language constructs, there
is one situation where sometimes I find D not handy. This code
can't work:
do {
const x = ...;
} while (predicate(x));
You need to use:
T x;
do {
x = ...;
} while (predicate(x));
Yeah. That comes from C/C++ (and is the same in Java and C#, I
believe). I
don't know why it works that way. It's definitely annoying.
[SNIP]
- Jonathan M Davis
Because it's the only way to guarantee that x exits when you reach the
end of the loop.
do {
if(true) continue; //Yawn... skip.
const x = ... ;
} while (predicate(x)); //What's x?
Basic goto limitations. Unlike goto though, inserting a "continue"
should never create a compile error, so the compiler *has* to guarantee
that the if condition references nothing inside its own block.
It is annoying, but nothing that can't be fixed with a scope bloc.
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();
}
rather than by a do-while loop. But it's a bit ugly, and doesn't enforce
a single break. IMHO it's a shame we've gone with the fairly useless
do-while, rather than cleaning up the syntax for loop-and-a-half.
I don't think the proposed changes bring us much closer to a useful
language construct.