Thread overview
Create alias of same name in inner scope, bug or feature?
Aug 14, 2021
Tejas
Aug 14, 2021
Mike Parker
Aug 14, 2021
Tejas
Aug 14, 2021
user1234
Aug 14, 2021
Tejas
August 14, 2021
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));

}
August 14, 2021

On Saturday, 14 August 2021 at 03:47:05 UTC, Tejas wrote:

>
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:

void main()
   macro_1(); // the local symbol
   .macro_1(); // the external symbol
}
August 14, 2021

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:

>
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:

void main()
   macro_1(); // the local symbol
   .macro_1(); // the external symbol
}

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!

August 14, 2021

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.

August 14, 2021

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