Wouldn't the compiler be smart with this shadowing variable?

Matheus matheus at gmail.com
Fri Dec 10 13:35:37 UTC 2021


Hi,

Wouldn't the compiler be smart with this shadowing variable, 
example:

void main(){
     int j;
     for(int i=0,j=0;i<10;++i){}
     return;
}

onlineapp.d(3): Error: variable `j` is shadowing variable 
`onlineapp.main.j`

So in the "for loop" shouldn't "i" be declared and "j" just be 
assigned to 0?

I mean this is a bit weird, imagine I have different "for loops" 
and in this situation I'll need to do this:

j=0;
for(int i=0;i<10;++i){}

Otherwise:

for(i=0,j=0;i<10;++i){}

This works, but now "i" variable will be out of the "for loop" 
scope.

Matheus.


More information about the Digitalmars-d-learn mailing list