Proposal: a MatchParameterNames attribute for delegates #3564
Replies: 6 comments
-
Can you provide any use cases where this would actually be useful? Given the massive amount of functional code within LINQ and similar projects there seems to be little issue with managing overloads that accept delegates intended to accept lambdas. IMO, parameter names are not a part of the signature of a method or a delegate. Developers should be able to name them freely based on their needs, and changing those names should not change the behavior of the source. |
Beta Was this translation helpful? Give feedback.
-
If implemented, renaming parameter names would become an API breaking change - currently we're free to rename parameter names without breaking consumers. This seems like a situation where a custom analyzer would be a perfect fit, allowing you to easily opt-in to these checks without imposing the constraint on the entire C# ecosystem. |
Beta Was this translation helpful? Give feedback.
-
Renaming parameters is currently a breaking change. |
Beta Was this translation helpful? Give feedback.
-
The real use case why I need this is for code generation. Based on the names of the lambda parameters, I can generate a new extension method with a new delegate type. If someone writes
I can generate an overload of Method1 that returns the following new type:
I will do that by generating a new delegate:
And generating an overload that takes If the user uses different lambda parameter names, a new overload will be generated and the two invocations of This is not the exact use case. But it captures the idea. In summary, this will enable source generation scenarios where lambda parameter names participate in the result of the generation. |
Beta Was this translation helpful? Give feedback.
-
This is an issue only for delegates with the
Currently, analyzers enable me to prevent the user from specifying the wrong names, but it doesn't force c# to use a different method overload. A compiler seam (like analyzers and source generators) where we can participate in overload resolution (e.g. to reject some overloads that the compiler selects and force it to look for the next best thing) will work. |
Beta Was this translation helpful? Give feedback.
-
Well, I can say that definitely won't happen. C# users expect overload resolution to behave consistently, allowing 3rd parties to interact with the algorithm is the opposite of consistent. This seems to me like a good use case for analyzers. Just issue an error when parameter names don't line up. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am proposing to limit the methods that a custom delegate can point to if the delegate has a custom attribute, e.g. called MatchParameterNames.
Example:
If there is a method group with two overloads like this:
And you call
M3(b =>{})
, then the first overload of M3 will not be considered because the lambda is actually a method with a parameter calledb
and thus cannot be assigned toDelegate1
. In this case, the second overload will be used.This feature can be used if a library author wants to force users of the library to use certain names of lambda parameters.
Beta Was this translation helpful? Give feedback.
All reactions