www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - enhance do...while scope

reply Kramer <Kramer_member pathlink.com> writes:
I know this has been suggested before, but I think it would be really nice if
the while() at the end of a do...while loop could have visibilty to variables in
the loop.

It's a minor thing and it doesn't hinder coding, but I think this:






is nicer than:







I think it just brings the code closer to what the programmer intended.  I think
that's one of D's goals, no?

-Kramer
Jun 03 2005
next sibling parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
/me suggested it as well... It still gets my vote... Walter ? :)


Kramer wrote:
 I know this has been suggested before, but I think it would be really nice if
 the while() at the end of a do...while loop could have visibilty to variables
in
 the loop.
 
 (...)
 




-- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Jun 03 2005
parent Trevor Parscal <trevorparscal hotmail.com> writes:
Tom S wrote:
 /me suggested it as well... It still gets my vote... Walter ? :)
 
 
 Kramer wrote:
 
 I know this has been suggested before, but I think it would be really 
 nice if
 the while() at the end of a do...while loop could have visibilty to 
 variables in
 the loop.

 (...)





easy to read, and consistent with other loops. :) Trevor Parscal votes YES! -- Thanks, Trevor Parscal www.trevorparscal.com trevorparscal hotmail.com
Jun 03 2005
prev sibling parent reply clayasaurus <clayasaurus gmail.com> writes:
Kramer wrote:
 I know this has been suggested before, but I think it would be really nice if
 the while() at the end of a do...while loop could have visibilty to variables
in
 the loop.
 
 It's a minor thing and it doesn't hinder coding, but I think this:
 




 
 is nicer than:
 





 
 I think it just brings the code closer to what the programmer intended.  I
think
 that's one of D's goals, no?
 
 -Kramer
 
 
What about this potential situation... char[] input; do { char[] input = readLine(); } while (input) Should the compiler not compile this?
Jun 03 2005
next sibling parent Trevor Parscal <trevorparscal hotmail.com> writes:
clayasaurus wrote:
 Kramer wrote:
 
 I know this has been suggested before, but I think it would be really 
 nice if
 the while() at the end of a do...while loop could have visibilty to 
 variables in
 the loop.

 It's a minor thing and it doesn't hinder coding, but I think this:






 is nicer than:







 I think it just brings the code closer to what the programmer 
 intended.  I think
 that's one of D's goals, no?

 -Kramer
What about this potential situation... char[] input; do { char[] input = readLine(); } while (input) Should the compiler not compile this?
it should act no different than int i = 50; for(int i = 0; i < 3; i++) { writefln(itoa(i)); } /// output 0 1 2 /// I don't think this will break any code. The scope of the (condition) in the while should just be the same scope as between the { .. } of the do. -- Thanks, Trevor Parscal www.trevorparscal.com trevorparscal hotmail.com
Jun 03 2005
prev sibling next sibling parent Brad Beveridge <brad somewhere.net> writes:
clayasaurus wrote:
 Kramer wrote:
 
 I know this has been suggested before, but I think it would be really 
 nice if
 the while() at the end of a do...while loop could have visibilty to 
 variables in
 the loop.

 It's a minor thing and it doesn't hinder coding, but I think this:






 is nicer than:







 I think it just brings the code closer to what the programmer 
 intended.  I think
 that's one of D's goals, no?

 -Kramer
What about this potential situation... char[] input; do { char[] input = readLine(); } while (input) Should the compiler not compile this?
The same thing should happen as if this happened { int x; while(1) { int x; x = 1; // inner-most scope x is used } } At least, that makes the most sense to me. Brad
Jun 03 2005
prev sibling next sibling parent reply Kramer <Kramer_member pathlink.com> writes:
I believe it should, but the variable in the while(...) should be scoped to the
lexically enclosing braces.  So, in the case that there was no /input/ variable
in the while(...), the compiler would find the definition outside of the
do...while.  Isn't that how other scoping issues are handled - can't find it in
the most enclosing scope, go up?

-Kramer

In article <d7qgb1$1bfb$1 digitaldaemon.com>, clayasaurus says...
Kramer wrote:
 I know this has been suggested before, but I think it would be really nice if
 the while() at the end of a do...while loop could have visibilty to variables
in
 the loop.
 
 It's a minor thing and it doesn't hinder coding, but I think this:
 




 
 is nicer than:
 





 
 I think it just brings the code closer to what the programmer intended.  I
think
 that's one of D's goals, no?
 
 -Kramer
 
 
What about this potential situation... char[] input; do { char[] input = readLine(); } while (input) Should the compiler not compile this?
Jun 03 2005
parent Trevor Parscal <trevorparscal hotmail.com> writes:
Kramer wrote:
 I believe it should, but the variable in the while(...) should be scoped to the
 lexically enclosing braces.  So, in the case that there was no /input/ variable
 in the while(...), the compiler would find the definition outside of the
 do...while.  Isn't that how other scoping issues are handled - can't find it in
 the most enclosing scope, go up?
Always so long as I have been programming. -- Thanks, Trevor Parscal www.trevorparscal.com trevorparscal hotmail.com
Jun 03 2005
prev sibling parent "Regan Heath" <regan netwin.co.nz> writes:
On Fri, 03 Jun 2005 16:59:30 -0400, clayasaurus <clayasaurus gmail.com>  
wrote:
 Kramer wrote:
 I know this has been suggested before, but I think it would be really  
 nice if
 the while() at the end of a do...while loop could have visibilty to  
 variables in
 the loop.
  It's a minor thing and it doesn't hinder coding, but I think this:




  is nicer than:





  I think it just brings the code closer to what the programmer  
 intended.  I think
 that's one of D's goals, no?
  -Kramer
What about this potential situation... char[] input; do { char[] input = readLine(); } while (input) Should the compiler not compile this?
Good point. Good replies. Assuming the change is made, and behaviour updated as suggested in the replies... I'd just like to add that this request changes the behaviour of do/while such that it no longer behaves like it does in C/C++ (and Java?). So, when porting code, copying a do/while loop to D could (in cases like the above) cause different behaviour to the original intent. So, while I like this idea, and think the 3 replies/answers are the "way to go"(tm) I wanted to note that it will break consistency with C/C++ (and maybe Java) and for those reasons Walter might not go for it, we'll just have to see. Regan
Jun 03 2005