-
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
[Proposal]: Allow nameof to always access instance members from static context (VS 17.7, .NET 8) #4037
Comments
If you change P.Length to C.P.Length, it compiles, which to my mind argues that it is a bug. Instance or not shouldn’t ever impact nameof, that’s just confusing. |
I have an implementation at dotnet/roslyn#48754 |
I know c# isn't vb, but I was extremely surprised to find out that the equivalent of: Sub Main
Dim d = New Dictionary(Of String, String)
Console.WriteLine(Nameof(d.First().Key))
End Sub void Main()
{
var d = new Dictionary<string, string>();
Console.WriteLine(nameof(d.First().Key));
} doesn't work. |
This doesn't compile in VB either though: https://sharplab.io/#v2:EYLgtghglgdgNAGxAN2HALiKC4BMQDUAPgJJgAOA9gE7oDOABAMoCed6ApmALABQZVWo1bsuAOgDClBAg4BjdFEow6YgOIcYHalDl8+ABQCuwBLoYSEEOowl8GD5iYYBZaDHuOvAEShgGuAwAvAwAchwA7gy+CkowENQsABQA8gBmzOg6MADmcJnZOQCUnl4OUirSHGIA6jqcADKwHEmhEGAc6Um4YgBiUNTsSUViANIcLEUlvF4AojCBTCZ884GW1nRAA==. |
I think no update to C# spec is needed. The quote in this issue comes from closed PR dotnet/csharpstandard#10. The current spec looks fine: §11.7.20 Nameof expressions. |
@333fred :
If you use: Sub Main
Dim d = New Dictionary(Of String, String)
Console.WriteLine(NameOf(d.First.Key))
End Sub |
Same with open generic types.
|
That is quite a wart. Discussion: #2547 |
To keep the spec simple the design of |
Allow nameof to always access instance members from static context
Summary
For background see dotnet/roslyn#48754
The current compiler has some interesting behavior with regards to accessing instance members from a static context:
Where even though
P
is an instance member you can access it from a static context, but you can't accessP.Length
.The relevant part of the spec is this:
This is somewhat ambiguous, but can be interpreted as saying only the top level lookup ignores errors where an instance member was found in a static context, but nested lookups do not. See here dotnet/roslyn#48754 (comment).
My request here is twofold:
Motivation
Detailed design
Update the spec from:
To
Drawbacks
This is not a particularly common scenario, and so probably not worth changing the spec for, if it's decided that the current implementation is not a bug.
Alternatives
Unresolved questions
Design meetings
https://github.com/dotnet/csharplang/blob/master/meetings/2020/LDM-2020-11-11.md#allow-nameof-to-access-instance-members-from-static-contexts
The text was updated successfully, but these errors were encountered: