View mode: basic / threaded / horizontal-split · Log in · Help
March 31, 2005
especially for..
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
March 31, 2005
Re: especially for..
Actually, it's not a bad idea at all. I like it.
March 31, 2005
Re: especially for..
Walter wrote:
> Actually, it's not a bad idea at all. I like it.

*faints*

...

Just joking.

-- Chris Sauls
March 31, 2005
Re: especially for..
"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..
March 31, 2005
Re: especially for..
// 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 +/
March 31, 2005
Re: especially for..
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
April 01, 2005
Re: especially for..
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 +/
April 01, 2005
Re: especially for..
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 +/
>
>
April 01, 2005
Re: especially for..
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 +/
Top | Discussion index | About this forum | D home