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 Question: Allow lambda conversion despite optional parameter or params differences #6651

Closed
jcouv opened this issue Nov 2, 2022 · 1 comment
Assignees
Labels
Proposal Question Question to be discussed in LDM related to a proposal

Comments

@jcouv
Copy link
Member

jcouv commented Nov 2, 2022

As part of supporting default parameter values in lambdas (proposal, spec), we had decided to disallow the conversion from a lambda to a delegate type when there is a mismatch between default values or params modifiers.

For example:

delegate void D(int x, int y = 0, int[] z);
D d = (int a = 1, int b = 2, params int[] c) => { }; // conversion fails and error reported on all parameters

I'm proposing we revisit that decision to allow that conversion and merely warn on such lambda parameter differences.

Rationale

It seems more consistent to allow the conversion, just like we allow method group conversions with similar differences, because we know how to convert.

Also, there's a bit of a parallel with tuple conversions, where we merely warn for specifying different names in a tuple literal which are useless, but we allow the conversion from a tuple type and a tuple literal despite such name differences:

var t = (a: 1, b: 2);
(int x, int y) t2 = t;            // OK
(int x, int y) t3 = (a: 1, b: 2); // Warning on both elements

Note that he failure/absence of conversion can affect overload resolution:

void M(Delegate1 d1) { }
void M(Delegate2 d2) { }
M((int a = 1, int b = 2, params int[] c) => { }); // could bind to the first overload, the second overload, or both (ambiguity) depending on existence of conversion
@jcouv jcouv self-assigned this Nov 2, 2022
@333fred 333fred changed the title Proposal: Allow lambda conversion despite optional parameter or params differences Proposal Question: Allow lambda conversion despite optional parameter or params differences Nov 2, 2022
@333fred 333fred added the Proposal Question Question to be discussed in LDM related to a proposal label Nov 2, 2022
@333fred
Copy link
Member

333fred commented Nov 3, 2022

Discussed in LDM on November 2nd: https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-11-02.md#default-parameter-values-in-lambdas

Conclusion

The existence of conversions from lambdas to delegate types will be unaffected by params or default parameter values. Differences in those will cause a warning later in the pipeline. Lambdas that do not have params/default parameter values will not warn when converted to delegate types with them. We will not warn for method group conversions that differ by params or default parameter values.

@333fred 333fred closed this as completed Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Proposal Question Question to be discussed in LDM related to a proposal
Projects
None yet
Development

No branches or pull requests

2 participants