Discussion: mixed initializer styles #2616
Replies: 13 comments
-
I find this a little confusing: using System.Collections.Generic;
public class C {
public void M() {
var list = new List<int>
{
Capacity = 100,
1,
2,
3,
};
}
} At first glance it looks like there's 4 items in the list, but actually theres 3. However it could well be that if I got a little more used to this, I wouldn't find it so strange. But I think it would be nice to do some user studies on that. |
Beta Was this translation helpful? Give feedback.
-
That case raises an ambiguity that currently is avoided precisely because you could either have a collection initializer or an object initializer, e.g. with the name |
Beta Was this translation helpful? Give feedback.
-
int Capacity;
var list = new List<int>
{
Capacity = 100,
1,
2,
3,
}; Does not compile today. So it would not be a breaking change. |
Beta Was this translation helpful? Give feedback.
-
But this does: int Capacity;
var list = new List<int>
{
(Capacity = 100),
1,
2,
3,
}; It might be a bad idea for them to have two separate meanings, but it's rare enough I don't think it matters. |
Beta Was this translation helpful? Give feedback.
-
Those two separate meanings are already present, however, not at the same time. int Capacity;
var l1 = new List<int>
{
Capacity = 100 // l1.Capacity = 100;
};
var l2 = new List<int>
{
(Capacity = 100) // l2.Add(100);
}; |
Beta Was this translation helpful? Give feedback.
-
This way its not ambiguous though. var list = new List<int>(new [] { 1, 2, 3 }) { Capacity = 100 }; Maybe we should work on turning it into this: var list = new List<int>({ 1, 2, 3 }) { Capacity = 100 }; Target-typed collection initializer anyone? |
Beta Was this translation helpful? Give feedback.
-
@mattwar What should be the type of |
Beta Was this translation helpful? Give feedback.
-
It was supposed to be an array initializer for a target-typed array/collection. It was for the argument to List(IEnumerable). |
Beta Was this translation helpful? Give feedback.
-
@mattwar So a kind of array literal, right? Or you meant it to be interpreted differently in other contexts, e. g. |
Beta Was this translation helpful? Give feedback.
-
For this case List<int> x = { 1, 2, 3 }; would be interpreted as List<int> x = new List<int> { 1, 2, 3 }; But for cases where the target type is not a constructable class, but is assignable from array, like IEnumerable, IReadOnlyList, etc. IEnumerable<int> x = new int[] { 1, 2, 3 }; |
Beta Was this translation helpful? Give feedback.
-
@mattwar Thank you for the explanation! Nice idea, is there an github issue to track its progress anywhere? BTW I'd expect |
Beta Was this translation helpful? Give feedback.
-
@vladd not yet. |
Beta Was this translation helpful? Give feedback.
-
Using target-typed new, you can write List<int> x = new() { 1, 2, 3 }; If you don't mind Edit: Actually |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently both of these are valid initializers:
but not when they are mixed in a single initializer
Looks like the parser already produce a valid tree for this, but the compiler eventually rejects it.
Could we relax the rules so that this is permitted?
Beta Was this translation helpful? Give feedback.
All reactions