-
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
Collection expressions cannot be initialize if we specify the type #77056
Comments
public class ComplexCollection : StringCollection
{
public void Add(Rec v) => Add(v.ToString());
} ComplexCollection doesn't count as a collection here because it's iteration type is just 'string' (not 'Rec'). So you cannot add instances of 'Rec's to it. For that to be allowed, a language change would have to be made here. Such requests can happen at dotnet/csharplang. Thanks! |
If what you are saying is true... ComplexCollection com1 = [
"42",
new(42),
]; IS allowed, and it's translated correctly to ComplexCollection complexCollection = new ComplexCollection();
complexCollection.Add("42");
complexCollection.Add(new Rec(42)); But as soon as we will specify the type name near Both collection expression and collection initializers seems producing the same code (in this instance). {
"12",
new(42),
new Rec(42),
}; is allowed [
"42",
new(42),
//new Rec(42), // error CS0029: Cannot implicitly convert type 'Rec' to 'string'
]; not. |
It doesn't seem expected that adding a |
Reactivating. Missed that. Tagging in @cston |
This is a loophole in the collection expression spec. The collection expression conversion requires each element to be implicitly convertible to the element type, but when constructing the collection, we use the applicable For ComplexCollection com1 = [
"42",
new(42),
//new Rec(42), // error CS0029: Cannot implicitly convert type 'Rec' to 'string'
]; For the element
And for construction, However, for the explicitly typed element See also dotnet/csharplang#7724 It would be good to fix this spec loophole, and align conversion and construction, if we can do so simply and with minimal breaking change. |
Even if you will define public class ComplexCollection : StringCollection, IReadOnlyCollection<Rec>
{
public void Add(Rec v) => Add(v.ToString());
IEnumerator<Rec> IEnumerable<Rec>.GetEnumerator()
{
throw new NotImplementedException();
}
} You will not be able to use |
Version Used: 9.0.100 (but as well v8)
Steps to Reproduce:
Add
method, that support some other typeA minimal repro in sharplab.
Expected Behavior:
You should be able to initialize the object just specifying the
new()
AND specifying the typenew Rec()
Actual Behavior:
Specifying the type
new Rec()
is causing the compiler to complain...But if we omit the type (and allow compiler to figure it out), it will be allowed.
With complex type, we can use "workaround" and not specify the type, there is no way we can do that with simple types.
The example is with "combination" (when we pass both types - strings, and ints/Recs), but the issue exists even if we will be trying to use one type.
The text was updated successfully, but these errors were encountered: