-
See original post here: It seems that there is an issue with inferred delegate types, either in roslyn or in csharplang specification... Generally, I think we can reasonably expect that, if this can compile:
...Then this single assignment should compile as well:
However, this is not satisfied when inferred delegate types are involved. Consider the code below:
I have the impression that inferred delegate types imply an implicit conversion at some stage, which probably should not be the case. (link to the code: https://godbolt.org/z/3sbd9ndrT) I tried different compilers and searched for documentation, discussions, and potential known issues regarding the behavior of inferred delegate types, unsuccessfully. Can somebody please tell if this is a language/compiler defect, or if my expectations for these conversions to work are unreasonable? Thank you in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
This is expected. Inferred delegate types will re-use any existing // fine
Predicate<int> pred1 = (int x) => x % 2 == 0;
// not fine, pred is Func<int, bool>
var pred = (int x) => x % 2 == 0;
// error CS0029: Cannot implicitly convert type 'System.Func<int, bool>' to 'System.Predicate<int>'
Predicate<int> pred2 = pred; |
Beta Was this translation helpful? Give feedback.
-
This is not the case. There are many situations where single assignment can't be splitted with
|
Beta Was this translation helpful? Give feedback.
This is expected.
Inferred delegate types will re-use any existing
Action<...>
/Func<....>
delegate if the signature matches. Otherwise, C# will create its own delegate type. It's impossible to infer the natural delegate type for a lambda otherwise as delegates are nominally typed and do not have signature equivalence.See: https://github.com/dotnet/csharplang/blob/main/proposals/csha…