www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - especially for..

reply xs0 <xs0 xs0.com> writes:
I propose some new syntax sugar:

if (int result=blah()) {}
while (int result=blah()) {}

I mean, why is declaring new variables limited to for() and foreach()?

The problem is not merely more typing, but also scoping. If you want to 
make sure a var is not used after a while loop, you have to do it like:

while(1) {
    int result=blah();
    if (!result) break;
}

or, even uglier:


{int result;
while (result=blah()) {
       ..;
}}


which doesn't even work, because it says that = doesn't produce a boolean...

if ((int value=blah())>maximum_allowed)
     error("Value above maximum", value);
else
     success("All right", value);

Wouldn't this be nice? :)


xs0
Mar 31 2005
next sibling parent reply "Walter" <newshound digitalmars.com> writes:
Actually, it's not a bad idea at all. I like it.
Mar 31 2005
next sibling parent Chris Sauls <ibisbasenji gmail.com> writes:
Walter wrote:
 Actually, it's not a bad idea at all. I like it.
*faints* ... Just joking. -- Chris Sauls
Mar 31 2005
prev sibling parent "Regan Heath" <regan netwin.co.nz> writes:
On Thu, 31 Mar 2005 10:37:37 -0800, Walter <newshound digitalmars.com>  
wrote:
 Actually, it's not a bad idea at all. I like it.
On this subject, I noticed this the other day: void main() { int a; int b; for(a = b, int i = 5; true; i++) {} } declaring the 'int' after a ',' is not allowed. Should it be? can it be? I realise in most cases you can reverse the order and declare the int first. In the case I had the int was assigned to 'a' in other words the result of the first part (so it needed to be done first). I resolved this by moving the "a = b" to outside the for loop. So, it's not a big problem, but it would be "nice". Regan
Mar 31 2005
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"xs0" <xs0 xs0.com> wrote in message news:d2gsnq$une$1 digitaldaemon.com...
 if (int result=blah()) {}
 while (int result=blah()) {}
Ooh, would that cut down on declaring dumb temp values before conditionals..
Mar 31 2005
parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
// shameless plug:

Lemme remind another proposal - regarding do .. while, so that the 
following code would be valid:

	do {
		int x = getchar();
	} while (x != 'x');

Basically the scope of the variables declared inside the block wouldn't 
end with the '}' but with the ';' after 'while'.


-- 
Tomasz Stachowiak  /+ a.k.a. h3r3tic +/
Mar 31 2005
parent reply "Lionello Lunesu" <lio lunesu.removethis.com> writes:
Mentioned before, and still a good idea.. Got my vote.

L.

"Tom S" <h3r3tic remove.mat.uni.torun.pl> wrote in message 
news:d2hnr4$1sqn$1 digitaldaemon.com...
 // shameless plug:

 Lemme remind another proposal - regarding do .. while, so that the 
 following code would be valid:

 do {
 int x = getchar();
 } while (x != 'x');

 Basically the scope of the variables declared inside the block wouldn't 
 end with the '}' but with the ';' after 'while'.


 -- 
 Tomasz Stachowiak  /+ a.k.a. h3r3tic +/ 
Mar 31 2005
parent reply "Alejandro Lapeyre" <AlejandroLapeyre jotmail.com> writes:
no,no,no,no

A bracket is a bracket.
You use brackets because you want to delimit a phrase, an idea. You can not 
be allowed to refer to an object that is declared in brackets from outside 
the brackets, it is out of scope.

It is like (I do have a dog) saying it likes peanuts.

Best Regards,
Alejandro Lapeyre


"Lionello Lunesu" <lio lunesu.removethis.com> escribió en el mensaje 
news:d2iomk$2vi2$1 digitaldaemon.com...
 Mentioned before, and still a good idea.. Got my vote.

 L.

 "Tom S" <h3r3tic remove.mat.uni.torun.pl> wrote in message 
 news:d2hnr4$1sqn$1 digitaldaemon.com...
 // shameless plug:

 Lemme remind another proposal - regarding do .. while, so that the 
 following code would be valid:

 do {
 int x = getchar();
 } while (x != 'x');

 Basically the scope of the variables declared inside the block wouldn't 
 end with the '}' but with the ';' after 'while'.


 -- 
 Tomasz Stachowiak  /+ a.k.a. h3r3tic +/
Apr 01 2005
parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Alejandro Lapeyre wrote:
 no,no,no,no
 
 A bracket is a bracket.
 You use brackets because you want to delimit a phrase, an idea. You can not 
 be allowed to refer to an object that is declared in brackets from outside 
 the brackets, it is out of scope.
 
 It is like (I do have a dog) saying it likes peanuts.
o_0 It's not about allowing the names from the inner scope to be accessed somewhere far from the 'brackets', but in the while() 'phrase' following the block, which is logically connected to the block of code it encapsulates. With the proposal this is valid: do { int x = getchar(); } while (x != 'x'); But this is still invalid: do { int x = getchar(); } while (x != 'x'); x = 1; // Can't see x here It's only about extending the inner scope to the parentheses of the 'do .. while' loop's condition test. And it actually supports the concept of enclosing the 'idea' or 'phrase' as you call it, so that you don't have to define dummy variables like: int x; do { x = getchar(); } while (x != 'x'); // x accessible here and be forced to put them in another brace pairs in order not to spoil the namespace: { int x; do { x = getchar(); } while (x != 'x'); }
 "Lionello Lunesu" <lio lunesu.removethis.com> escribió en el mensaje 
 news:d2iomk$2vi2$1 digitaldaemon.com...
 
Mentioned before, and still a good idea.. Got my vote.

L.

"Tom S" <h3r3tic remove.mat.uni.torun.pl> wrote in message 
news:d2hnr4$1sqn$1 digitaldaemon.com...

// shameless plug:

Lemme remind another proposal - regarding do .. while, so that the 
following code would be valid:

do {
int x = getchar();
} while (x != 'x');

Basically the scope of the variables declared inside the block wouldn't 
end with the '}' but with the ';' after 'while'.
-- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Apr 01 2005