-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Type inference for lambdas in collection expressions #73256
Comments
This looks to be a regression from 17.9. In 17.9, for collection types implementing |
What rule changed that caused this? I thought we were effectively rolling back to the 17.9 rules (just looking for the presence of an Add method now). |
The That additional check in 17.10 that the element is implicitly convertible to |
@ufcpp, thanks for reporting this issue. The break was the result of an intentional design change for collection expressions targeting types that implement We had not recognized your breaking change case previously. I’ve added an item based on your example to the breaking change documentation for 17.10 (see #73278). Did you hit this issue with an existing collection type? We're curious how common this case might be. Thanks. |
Yes, this change broke my team's code. Extracting the main points, we had the following classes. It used collection initialisers for event handler registration. using System.Collections;
Reciever reciever = new()
{
(sender, args) =>
{
},
(sender, args, args2) =>
{
}
};
class Reciever : IEnumerable
{
public void Recieve()
{
RecievedEventArgs args = new();
_onRecieved1?.Invoke(this, args);
if (_onRecieved2 is { } r)
{
RecievedEventArgs2 args2 = new(); // high computational cost
_onRecieved2?.Invoke(this, args, args2);
}
}
private Action<Reciever, RecievedEventArgs>? _onRecieved1;
private Action<Reciever, RecievedEventArgs, RecievedEventArgs2>? _onRecieved2;
public void Add(Action<Reciever, RecievedEventArgs> onRecieved) => _onRecieved1 += onRecieved;
public void Add(Action<Reciever, RecievedEventArgs, RecievedEventArgs2> onRecieved) => _onRecieved2 += onRecieved;
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException("dummy for collection initializers");
}
// normal args
struct RecievedEventArgs;
// args with high computational cost
struct RecievedEventArgs2; VS 17.9 suggested IDE0028 (and 17.10 still makes this suggestion). With this applied, we had the code like the following. Reciever reciever =
[
(sender, args) =>
{
},
(sender, args, args2) =>
{
}
]; Since there are two overloads of |
Sorry about the hassle! |
Version Used:
Visual Studio 17.10.0 Preview 5.0
Steps to Reproduce:
Diagnostic Id:
CS8917
Expected Behavior:
No error.
Actual Behavior:
No error in Visual Studio 17.10 Preview 4, but CS8917 in 17.10 Preview 5.
On the other hand, it can be compiled if you use a collection initializer:
A a = new() { _ => { } };
.The text was updated successfully, but these errors were encountered: