Proposal: Expand method type inference to more use cases #10194
Unanswered
Corey-M
asked this question in
Language Ideas
Replies: 1 comment
-
|
Dupe of #9006 |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
Summary
Methods may not be the direct target of extensions or switch expressions, cannot be returned from expressions and so on. This should be changed to allow more direct usage of methods.
Description
In general a method cannot be the input or output of any expression except conversion (implicit or explicit) to a delegate type. We can assign (for simple example)
Path.GetFileName(string path)to aFunc<string, string>delegate, or we can cast it with(Func<string, string>). We can then consume that delegate in expressions, return it as the result of some expression, use it as the target of an extension and other things we cannot do directly with the method itself.We can also use type inference on the method to assign it to a
var, with the predictable result that the assigned variable will be of typeFunc<string, string>.There are a variety of cases where extensions and expressions are useful with delegate types, but using those same extensions/expressions with methods is expressly forbidden. I propose that this be relaxed somewhat by using the same type inference as used for the implicitly typed delegate to implicitly convert the method inline.
Use Cases
Switch Expressions
Methods currently require casting to delegate explicitly or implicitly to be used as the results of a switch expression:
I propose that all usages like this be treated as though implicitly typed and converted. In this case the two failed samples would succeed as if they were explicitly cast. Sample 4-6 would ideally have identical output code.
Extensions
My primary interest for extension methods written for delegate types to work on methods directly without casting.
In this example when attempting to use
M1orM2in a member access expression (i.e. the.symbol is encountered) the method should be implicitly cast to the inferred delegate typeFunc<string, int>before continuing, using the same methodology as assigning to an implicitly typed local.Notes
varassignment, it won't work here.Beta Was this translation helpful? Give feedback.
All reactions