Thread overview
Why is a variable allowed to be declared twice?
Mar 09, 2005
jicman
Mar 09, 2005
brad
Mar 09, 2005
Matthew
Mar 09, 2005
Ant
Mar 09, 2005
jicman
March 09, 2005
As in,

import std.stdio;
void main()
{
int i;
for (int i=0;i<2;i++)
writefln(i);
}

Should not have the compiler complained about it being declared twice?

I missed this one and it caused me. :-)

thanks,

josé


March 09, 2005
jicman wrote:
> As in,
> 
> import std.stdio;
> void main()
> {
> int i;
> for (int i=0;i<2;i++)
> writefln(i);
> }
> 
> Should not have the compiler complained about it being declared twice?
> 
> I missed this one and it caused me. :-)
> 
> thanks,
> 
> josé
> 
> 
No, the int i inside the for loop has a different scope.  This is the same as C++, and infact g++ doesn't even warn about i being hiding with -Wall on.
On a standards compilant C++ compiler (which MSVC by default is not) this will break
<code>
for (int i=0; i < 10; i++)
;
printf("%i\n", i);  // should be an error because i is no longer in scope.
</code>

D has the same scoping rules for "for" loops.

Brad
March 09, 2005
Most/all decent C++ compilers warn about this ...


<brad@domain.invalid> wrote in message news:d0lrc9$12oo$1@digitaldaemon.com...
> jicman wrote:
>> As in,
>>
>> import std.stdio;
>> void main()
>> {
>> int i;
>> for (int i=0;i<2;i++)
>> writefln(i);
>> }
>>
>> Should not have the compiler complained about it being declared twice?
>>
>> I missed this one and it caused me. :-)
>>
>> thanks,
>>
>> josé
>>
>>
> No, the int i inside the for loop has a different scope.  This is the
> same as C++, and infact g++ doesn't even warn about i being hiding
> with -Wall on.
> On a standards compilant C++ compiler (which MSVC by default is not)
> this will break
> <code>
> for (int i=0; i < 10; i++)
> ;
> printf("%i\n", i);  // should be an error because i is no longer in
> scope.
> </code>
>
> D has the same scoping rules for "for" loops.
>
> Brad


March 09, 2005
On Wed, 09 Mar 2005 16:43:34 +1300, brad wrote:

but this looks like it shouldn't be valid
(http://www.digitalmars.com/d/statement.html#block):

import std.stdio;
void main(char[][] args)
{
	int i = 25;
	{
		int args;
	}
	{
		int i = 15;
		writefln("in ",i);
	}
	for (int i=0; i<2; i++)
	{
		writefln("for ",i);
		for (int i=425; i<427; i++)
		{
			writefln("\tfor ",i);
		}
	}
	writefln("out ",i);
}


#outputs#
in 15
for 0
	for 425
	for 426
for 1
	for 425
	for 426
out 25
##

Ant

March 09, 2005
yeah, well, I was right... :-)

Ant says...
>
>On Wed, 09 Mar 2005 16:43:34 +1300, brad wrote:
>
>but this looks like it shouldn't be valid
>(http://www.digitalmars.com/d/statement.html#block):
>
>import std.stdio;
>void main(char[][] args)
>{
>	int i = 25;
>	{
>		int args;
>	}
>	{
>		int i = 15;
>		writefln("in ",i);
>	}
>	for (int i=0; i<2; i++)
>	{
>		writefln("for ",i);
>		for (int i=425; i<427; i++)
>		{
>			writefln("\tfor ",i);
>		}
>	}
>	writefln("out ",i);
>}
>
>
>#outputs#
>in 15
>for 0
>	for 425
>	for 426
>for 1
>	for 425
>	for 426
>out 25
>##
>
>Ant
>