www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Create alias of same name in inner scope, bug or feature?

reply Tejas <notrealemail gmail.com> writes:
```d
import std;
auto abc(T)(auto ref T a, auto ref T b){
     return a+b;
}

auto def(T)(auto ref T a, auto ref T b){
         return a*b;

}
alias macro_1 = abc;
void main()
{
     writeln(macro_1(15, 20));
     alias macro_1 = def;// is this NOT considered variable 
shadowing?
     writeln(macro_1(100, 20));

}
```
Aug 13 2021
parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 14 August 2021 at 03:47:05 UTC, Tejas wrote:
 ```d
 import std;
 auto abc(T)(auto ref T a, auto ref T b){
     return a+b;
 }

 auto def(T)(auto ref T a, auto ref T b){
         return a*b;

 }
 alias macro_1 = abc;
 void main()
 {
     writeln(macro_1(15, 20));
     alias macro_1 = def;// is this NOT considered variable 
 shadowing?
     writeln(macro_1(100, 20));

 }
 ```
Shadowing local symbols is illegal. But it's okay for local symbols to have the same name as module-scope symbols. You can disambigbuate with [the module scope operator][1]: ```d void main() macro_1(); // the local symbol .macro_1(); // the external symbol } ``` [1]: https://dlang.org/spec/module.html#module_scope_operators
Aug 13 2021
parent reply Tejas <notrealemail gmail.com> writes:
On Saturday, 14 August 2021 at 04:01:31 UTC, Mike Parker wrote:
 On Saturday, 14 August 2021 at 03:47:05 UTC, Tejas wrote:
 ```d
 import std;
 auto abc(T)(auto ref T a, auto ref T b){
     return a+b;
 }

 auto def(T)(auto ref T a, auto ref T b){
         return a*b;

 }
 alias macro_1 = abc;
 void main()
 {
     writeln(macro_1(15, 20));
     alias macro_1 = def;// is this NOT considered variable 
 shadowing?
     writeln(macro_1(100, 20));

 }
 ```
Shadowing local symbols is illegal. But it's okay for local symbols to have the same name as module-scope symbols. You can disambigbuate with [the module scope operator][1]: ```d void main() macro_1(); // the local symbol .macro_1(); // the external symbol } ``` [1]: https://dlang.org/spec/module.html#module_scope_operators
Oh right, the ```.``` operator will reference variable in the _module_ scope, not just the _immediate outer scope_, that's why it is not considered shadowing in that case. Thank you very much!
Aug 13 2021
parent reply user1234 <user1234 12.de> writes:
On Saturday, 14 August 2021 at 04:09:34 UTC, Tejas wrote:
 [...]
 Oh right, the ```.``` operator will reference variable in the 
 _module_ scope, not just the _immediate outer scope_,
you can use the module name to disambiguate as well. To extend Mike answer, the general rule is that if you can distinguish two names there's no shadowing.
Aug 14 2021
parent Tejas <notrealemail gmail.com> writes:
On Saturday, 14 August 2021 at 08:23:20 UTC, user1234 wrote:
 On Saturday, 14 August 2021 at 04:09:34 UTC, Tejas wrote:
 [...]
 Oh right, the ```.``` operator will reference variable in the 
 _module_ scope, not just the _immediate outer scope_,
you can use the module name to disambiguate as well. To extend Mike answer, the general rule is that if you can distinguish two names there's no shadowing.
Understood
Aug 14 2021