-
Notifications
You must be signed in to change notification settings - Fork 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
Open issues for collection literals #7085
Comments
Partly discussed in LDM in https://github.com/dotnet/csharplang/blob/main/meetings/2023/LDM-2023-04-03.md#collection-literals. |
Is there a reason why an API like the one used for |
There's no reason why it wouldn't work. Howeve, the core question would be: is such complexity necessary? The ISH case has to deal with much more complex scenarios (including not wanting to do any work depending on the state of things at runtime). Collection literals are much simpler and thus can benefit from a much narrower and easier pattern around construction. |
I wish I could use this feature at initializer for list and dictionary var sarray = "0,1,2,3,4,5,6,7,8,9";
var list = new List<int>() {
1,
..sarray.Split(',').Select((x) => int.TryParse(x,out var n) ? n : -1).Where((n) => n > 1 && n < 9),
9
}; // result in 1,2,34,5,6,7,8,9 For dictionary, it should accept list of var sarray = "0,1,2,3,4,5,6,7,8,9";
var dict = new Dictionary<string,int>() {
["1"] = 1,
..sarray.Split(',').Select((x) => KeyValuePair.Create(x,int.TryParse(x,out var n) ? n : -1)).Where((pair) => pair.Value > 1 && pair.Value < 9),
["9"] = 9
}; This also allow us to make Dictionary initialize with conditional key existence |
THe feature already allows for that, just with shorter syntax. Specifically: var list = [
1,
..sarray.Split(',').Select((x) => int.TryParse(x,out var n) ? n : -1).Where((n) => n > 1 && n < 9),
9
]; // result in 1,2,34,5,6,7,8,9
// and
var dict = [
"1": 1,
..sarray.Split(',').Select((x) => KeyValuePair.Create(x,int.TryParse(x,out var n) ? n : -1)).Where((pair) => pair.Value > 1 && pair.Value < 9),
"9": 9
]; |
@CyrusNajmabadi Is the dict will become I would love to specified the type by myself |
@Thaina you can see the rules in the linekd proposal: https://github.com/dotnet/csharplang/blob/main/proposals/collection-literals.md
You can :) |
Thank you very much |
Open issues for collection literals
Open issues for collection literals to review at LDM (see proposal).
Support spread operator
Support a spread operator to inline an enumerable within a collection literal. The proposed syntax is
..e
Possible translation of
List<int> list = [x, ..e, y];
Avoid extra allocations when the enumerable type has a
Length
orCount
property, or whenTryGetNonEnumeratedCount<T>(this IEnumerable<T>, out int)
returns true.Avoid intermediate collections when not observable.
For instance, avoid generating collections for the conditional expression
b ? [1, 2, 3] : []
below.Syntax ambiguity between spreads and ranges in a collection literal. (Since the ambiguity is within a collection literal, this is not a compatibility issue.)
(..e)
or include a start index0..e
for a range, or...
) for spread. (Lack of consistency with slice patterns.)Support dictionaries and dictionary elements
Support collection literals that represent dictionaries, with a simple syntax for key-value pairs. The proposed syntax for key-value pairs is
k:v
Possible translation of
var d = [k1:v1, k2:v2];
Interfaces
I<TKey, TValue>
implemented byDictionary<TKey, TValue>
can be used as target types for collection literals.The natural type of a collection literal is
Dictionary<TKey, TValue>
when the best common type of the elements isKeyValuePair<TKey, TValue>
.Construction of dictionary collection literals uses the indexer
this[TKey] { get; set; }
rather thanAdd(TKey, TValue)
, to provide consistent set semantics rather than add.Syntax ambiguity in a collection literal between a conditional expression and k:v with conditional access. (Since the ambiguity is within a collection literal, this is not a compatibility issue.)
Could bind to conditional access, based on precedence, and require parentheses for
a ? ([b]) : c
.Support
Construct
methodsAllow collection types with custom
Construct
methods (see proposal).This is primarily to allow efficient construction of immutable collections, and requires BCL changes to expose
Construct
methods on existing immutable collection types.A type
T
can be constructed from a collection literal through the use of avoid Construct(CollectionType)
method when:Construct
method is found on an instance ofT
(including extension methods?), andCollectionType
is some other type known to be a constructible type.To implement:
ImmutableArray<T> result = [x, ..e, y];
Implementing explicitly, using a builder:
Possible translation from collection literal using
init void ImmutableArray<T>.Construct(T[] values)
method:A type with suitable
Construct
method can be used as a target type for collection literals.May require an
init Construct()
method to ensure collection instance is not mutated after construction.Could we use constructors or factory methods instead?
For instance, with support for
params ReadOnlySpan<T>
:The text was updated successfully, but these errors were encountered: