Inferring delegate types for _lamdba_expressions_ and method groups through implicit conversion operators. #9521
Unanswered
mwadams
asked this question in
Language Ideas
Replies: 1 comment
-
I guess it is also interesting to note that the extra level of indirection is effectively resolved by the "new" wrapper. It is the "bridge" that lets us do inference right up the stack so we don't actually have any type names in that final example. But it would be great if the compiler could infer that bridge for us. |
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.
Uh oh!
There was an error while loading. Please reload this page.
-
There have been considerable improvements in lamdba_expressions type inference in recent versions of C#.
For example, given this type
it is now possible to resolve (using LangVersion preview at the time of writing)
However, if we introduce a union-like type:
And replace our
TestMethod()
with that union-like type:We can no longer implicitly resolve the lambda_expression to a specific delegate type, because it cannot make the double hop through the baked-in implicit conversion to a delegate type, and then the implicit conversion operator on the
Source
type.This forces us through the ugly mechanism of making our delegate constructors public and wrapping the lambda_expression in a
new()
. (Or a cast to the delegate type which is probably even more ugly. YMMV! :) )As was pointed out elsewhere, the blocker to this is actually that implicit conversions are (completely consistently) not considered when resolving target types. Which results in the same problem if you supply a method group, too (as there is a need for a hidden implicit conversion to the delegate type in that case too.)
However, I'm not approaching this suggestion from the "underlying mechanism" perspective - but from the "how you express your code in the language" perspective. Whatever the underlying mechanism, this would be considerably more concise and expressive.
I note that this would have the same issues given the v. future union language features as currently conceived.
Beta Was this translation helpful? Give feedback.
All reactions