Names and scope in D

Paul Backus snarwin at gmail.com
Sat Nov 14 17:54:17 UTC 2020


On Saturday, 14 November 2020 at 17:37:48 UTC, Dibyendu Majumdar 
wrote:
>
> If I do this, compiler accepts it:
>
> //int foo;
> struct foo {
>     int a;
> }
>
> void bar() {
>     foo foo;
>     foo.a = 5;
> }
>
> So if I read your reply correctly the variable 'foo' in bar() 
> doesn't conflict with the struct 'foo' because it is in a 
> different scope?

Yes, exactly. Symbols in an inner scope are (mostly) allowed to 
shadow symbols in an outer scope.

The one exception is that local variables are not allowed to 
shadow parameters:

void fun(int x)
{
     int x; // error
}


More information about the Digitalmars-d mailing list