View mode: basic / threaded / horizontal-split · Log in · Help
June 03, 2005
enhance do...while scope
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:

# do
# {
#     char[] input = std.stream.stdin.readLine();
# } while(input);

is nicer than:

# char[] input;
# do
# {
#     input = std.stream.stdin.readLine();
# } while(input);

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

-Kramer
June 03, 2005
Re: enhance do...while scope
/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.
> 
> (...)
> 
> # do
> # {
> #     char[] input = std.stream.stdin.readLine();
> # } while(input);


-- 
Tomasz Stachowiak  /+ a.k.a. h3r3tic +/
June 03, 2005
Re: enhance do...while scope
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.
>>
>> (...)
>>
>> # do
>> # {
>> #     char[] input = std.stream.stdin.readLine();
>> # } while(input);
> 
> 
> 

easy to read, and consistent with other loops. :)

Trevor Parscal votes YES!

-- 
Thanks,
Trevor Parscal
www.trevorparscal.com
trevorparscal@hotmail.com
June 03, 2005
Re: enhance do...while scope
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:
> 
> # do
> # {
> #     char[] input = std.stream.stdin.readLine();
> # } while(input);
> 
> is nicer than:
> 
> # char[] input;
> # do
> # {
> #     input = std.stream.stdin.readLine();
> # } while(input);
> 
> 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?
June 03, 2005
Re: enhance do...while scope
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:
>>
>> # do
>> # {
>> #     char[] input = std.stream.stdin.readLine();
>> # } while(input);
>>
>> is nicer than:
>>
>> # char[] input;
>> # do
>> # {
>> #     input = std.stream.stdin.readLine();
>> # } while(input);
>>
>> 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
June 03, 2005
Re: enhance do...while scope
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:
>>
>> # do
>> # {
>> #     char[] input = std.stream.stdin.readLine();
>> # } while(input);
>>
>> is nicer than:
>>
>> # char[] input;
>> # do
>> # {
>> #     input = std.stream.stdin.readLine();
>> # } while(input);
>>
>> 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
June 03, 2005
Re: enhance do...while scope
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:
>> 
>> # do
>> # {
>> #     char[] input = std.stream.stdin.readLine();
>> # } while(input);
>> 
>> is nicer than:
>> 
>> # char[] input;
>> # do
>> # {
>> #     input = std.stream.stdin.readLine();
>> # } while(input);
>> 
>> 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?
June 03, 2005
Re: enhance do...while scope
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
June 04, 2005
Re: enhance do...while scope
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:
>>  # do
>> # {
>> #     char[] input = std.stream.stdin.readLine();
>> # } while(input);
>>  is nicer than:
>>  # char[] input;
>> # do
>> # {
>> #     input = std.stream.stdin.readLine();
>> # } while(input);
>>  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
Top | Discussion index | About this forum | D home