Wouldn't the compiler be smart with this shadowing variable?
    Siarhei Siamashka 
    siarhei.siamashka at gmail.com
       
    Fri Dec 10 21:55:17 UTC 2021
    
    
  
On Friday, 10 December 2021 at 13:35:37 UTC, Matheus wrote:
> 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?
Intuitively, there are two possible interpretations here for 
inexperienced humans (and you seem to be favoring the latter):
    1. declare a new "j" variable to be visible only inside of the 
loop.
    2. reuse the existing "j" variable.
But only one of them is a correct description of what actually 
happens when the compiler processes this code. So it's a good 
thing that the compiler is smart enough to reject ambiguous code 
and prevent humans from making mistakes.
    
    
More information about the Digitalmars-d-learn
mailing list