-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add scoping rules for receiver parameters #9185
base: main
Are you sure you want to change the base?
Conversation
} | ||
``` | ||
|
||
In the example, `*1*` and `*2*` denote local variable declaration spaces, where `*2*` is nested within `*1*` and thus prohibited from introducing the same local variable names as `*1*`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either I am misinterpreting what this statement is saying, or it looks like it contradicts the current language rules, which allow shadowing of both parameters and locals within nested methods. For example, the following code is legal:
class C2(int p)
{
void M1(long p)
{}
static void M2(long p)
{}
void M3()
{
long p = 0;
}
static void M4()
{
long p = 0;
}
void M5(int p1)
{
string local1 (string p1) => p1;
local1("");
int l1 = 0;
string local2 ()
{
string p1 = "";
string l1 = "";
return p1 + l1;
}
local2();
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think shadowing of locals and parameters within local functions and lambdas was allowed in Roslyn time frame. It is quite possible this relaxation didn't make it into the spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the relevant proposal - https://github.com/dotnet/csharplang/blob/main/proposals/csharp-8.0/shadowing-in-nested-functions.md. With primary constructor parameters we followed the same rules, i.e. they can be shadowed by member names and by parameters and locals declared by members.
This adds scoping rules for receiver parameters. The upshot is that they should behave like primary constructor parameters: They are in scope within contained members, but it is illegal to reference them from a static context.