Skip to content
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

Proposal: Implicitly cast lambdas to matching signature delegates #9521

Closed
DavidArno opened this issue Mar 7, 2016 · 2 comments
Closed

Proposal: Implicitly cast lambdas to matching signature delegates #9521

DavidArno opened this issue Mar 7, 2016 · 2 comments

Comments

@DavidArno
Copy link

Consider the following code example for invoking a method in the display thread for a winforms application:

private void UpdateDisplay()
{
    Invoke(() => InThreadDisplayUpdate());
}

Currently, this code won't compile and must be written in one of the following two ways:

private void UpdateDisplay()
{
    Invoke((MethodInvoker)(() =>  InThreadDisplayUpdate()));
}

private void UpdateDisplay()
{
    Invoke((MethodInvoker) delegate { InThreadDisplayUpdate(); });
}

The signature of the lambda can easily be determined at compile-time and thus it would be possible to implicitly cast it to MethodInvoker (or have the compiler generate the required explicit cast).

@DavidArno DavidArno changed the title Proposal: Implicitly cast lambdas to be cast to matching signature delegates Proposal: Implicitly cast lambdas to matching signature delegates Mar 7, 2016
@HaloFour
Copy link

HaloFour commented Mar 7, 2016

Why would the compiler cast that delegate to MethodInvoker? The method doesn't accept a MethodInvoker, it accepts a Delegate. There is no specific delegate that it requires since it invokes the delegate dynamically. The issue is that the compiler has no idea what specific delegate to use without inference based on the target of the assignment and there is no signature equivalence of delegates in the CLR. #14

If there was an overload of Control.Invoke which accepted a MethodInvoker or Action or any specific delegate type then this would no longer be a problem.

@DavidArno
Copy link
Author

Ha, that'll teach me not to check the signature of Invoke properly. I thought I'd found a good example of what I was after. Back to the drawing board! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants