Proposal: Adjust Member Resolution Algorithm Priority Based on if the Receiver Is Static or an Instance #9525
Unanswered
LeQuackers
asked this question in
Language Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The Problem
A while ago I was playing around with the new C# 14 property extensions and ran into #9517. At glance it looks like a bug, but upon closer inspection it's actually caused by the current member resolution algorithm (see dotnet/roslyn#79309 (comment)).
Example of Problem Using C# 14 Extensions:
Non-Extension Example of Problem:
Essentially, the member resolution algorithm is finding the static method first because extensions are prioritized last. Checking for extensions last makes sense; however, in practice resolving to the static member first instead of the instance member on an instance isn't intuitive:
Proposed Solution
Prioritize members based on if the receiver is static or an instance, i.e.:
Example:
Current Risk
At the moment, if you use C# 14 extensions to add a property to a 3rd party type, there's a risk that your code will break if said 3rd party adds a method of the same name to that type in the future (i.e. minor version updates have the potential to break your code now), which can lock you into deprecated package versions if you're not careful. An example being a
bool IsAscii { get; }
instance extension property onchar
, which would work fine in .NET 4.8 and .NET Standard 2.0, but fails in .NET Core because thechar.IsAscii(char)
static method exists in later versions.Note that the current purposed solution only mitigates the risk by preventing instance members and static members from interfering with each other; if you have an instance extension property on a 3rd party type and they add an instance method of the same name later on, your code will still break.
Drawbacks
I haven't looked at the member resolution algorithm in-depth yet, but stable-sorting instance/static members to the front or needing to perform the member resolution algorithm twice would add overhead.
Breaking Changes
The proposed solution alters the member resolution algorithm, so I'd be surprised if it didn't break something. As an initial assessment, some cases were the compiler would give a CS8917 or CS0176 error would be valid code now. Getting some input/discussions on this would be a great help.
Alternatives
C# 14 extension properties allow the
new
keyword to be used on them, but it doesn't appear to do anything at the moment; it could probably be used to adjust its resolution priority somehow.todo (input/discussions welcome)
Prototype / Proof of Concept
todo
Beta Was this translation helpful? Give feedback.
All reactions