-
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 proposal for overload resolution priority #7906
Conversation
This is my replacement proposal for what I originally wrote up in dotnet#7707.
As the proposed OverloadResolutionPriorityAttribute has AttributeTargets.Method, I presume it cannot be applied to events, properties, or indexers, but can be applied to their accessors. Indexers can be overloaded but the proposed text changes only 12.8.9.2 (Method invocations), rather than 12.8.11.3 (Indexer access) or 12.6.4 (Overload resolution). Will the attribute on an accessor then be ignored, or will a diagnostic be required? (An indexer might have |
The file name looks wrong to me, and as such is annoyingly not triggering GitHub’s markdown renderer. |
With
Yes. Re. member types - I second @KalleOlaviNiemitalo 's concerns - the attribute should be applicable to methods, constructors, and indexer properties, but not to accessors ( PS. Overall, I like this more than the |
I'm not a fan of it being completely open ended either the numbers. I think there should be some predefined values/enums that help. Ie: |
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.
Just added my thoughts on a few aspects. Mostly looks good to me :)
I understand that this is a C# repo. However, in order to achieve the motivating goal, I think we need VB to pay attention to the attributes as well. Therefore, it would be good to create a VB specific proposal as well. |
Just to note that allowing overload priority could open a can of worms and shouldn't be rushed out to the public. I would instead recommend making |
I've had mixed feelings about that as well. I have kind of thought that this should be tagged as a "requires preview features" feature. I also really think that, in order to be truly useful, we need the c# compiler to allow methods that vary only by return type. I have plenty of methods I want to upgrade the return type on. |
Could |
I imagine that the compiler will follow the common pattern of not caring where the attribute comes from, only the fully qualified name of the attribute class. In that case, assuming that there is agreement on what the attribute name should be, the language can definitely ship with support for the feature without the BCL necessarily exposing one. But at the same time that means that someone could define their own and make use of this feature regardless of whether the BCL exposes it. |
This statement without specific examples is not actionable.
The BCL is not the only team that has found a strong need for this. |
At the moment the can of worms is empty |
Agree this is the most likely outcome but the spec should be clear about this. @333fred. |
I've also been wondering what people are referring to by "can of worms". It's strictly an opt-in feature; BCL maintainers generally do a great job at controlling what changes are approved. I don't expect anything "bad" to come out of it, except for potentially some invalid usage by 3-rd party libraries, but that can be said about almost any feature. |
No previous specification I'm aware of has been clear about this, I'm not sure what's different here? |
@AlekseyTs I've addressed your feedback on this. For VB, let's iron out what we want to do for C#, and then look at adapting it to VB. |
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.
LGTM (commit 4)
Does this also apply to extension methods? I think yes as the attribute applies on any method. Assuming we already have: class C1
{
public void M(int[] a) => Console.WriteLine("Array");
} Now we have a static class Ext
{
[OverloadResolutionPriority(1)]
public static void M(this C1 c, ReadOnlySpan<int> s) => Console.WriteLine("Span");
} IIRC However, what if we have class C1
{
[OverloadResolutionPriority(1)]
public void M(Span<int> s) => Console.WriteLine("Span");
[OverloadResolutionPriority(2)]
public void M(ReadOnlySpan<int> s) => Console.WriteLine("ReadOnlySpan");
public void M(int[] a) => Console.WriteLine("Array");
} and now a developer who doesn't own the type What if two extension methods from different type (or even different assembly) having with the same overload priority? static class Ext1
{
[OverloadResolutionPriority(1)]
public static void M(this C1 c, Span<int> s) => Console.WriteLine("Span");
}
static class Ext2
{
[OverloadResolutionPriority(1)]
public static void M(this C1 c, ReadOnlySpan<int> s) => Console.WriteLine("ReadOnlySpan");
} We have extension methods, and, in the future, we will also have extension types, there'll be many conflicts and ambiguities due to this if we introduce explicit overload priority. You have no way to prevent such conflicting happening as two extension methods to a same type can be from two different assemblies with no dependency on each other. This is one of what I said by the can of worms. |
No. Quoting from the proposal:
They can't without deriving from the type and adding a new overload, which they could already do.
No new conflicts or ambiguities exist. Extensions can already play games with namespace distance to influence priority, and Do you have any concerns not addressed by the proposal? |
Nope. I'm okay with it as long as the overload priority only affects the resolution of overloads which are defined in the type itself. |
I think they should affect Extension methods as well. This way I can upgrade those too. |
They do, but as worded currently, only within the same type. For example: static class Ext1
{
[OverloadResolutionPriority(1)]
public static void M(this C1 c, Span<int> s) => Console.WriteLine("Span");
[OverloadResolutionPriority(0)]
public static void M(this C1 c, ReadOnlySpan<int> s) => Console.WriteLine("ReadOnlySpan");
}
static class Ext2
{
[OverloadResolutionPriority(0)]
public static void M(this C1 c, ReadOnlySpan<int> s) => Console.WriteLine("ReadOnlySpan");
} Within |
I'm going to go ahead and merge this so we can put the open questions on the LDM agenda. |
This is my replacement proposal for what I originally wrote up in #7707.