Open
Description
openedon Oct 1, 2024
Background and motivation
We allowed collection expressions for IImmutableSet<T>
, IImmutableQueue<T>
, and IImmutableStack<T>
.
It seems odd to not support it on these types:
LinkedList<T>
Stack<T>
Queue<T>
- (
ISet<T>
) - (
IReadOnlySet<T>
)
We don't want to special case ISet<T>
and IReadOnlySet<T>
as the compiler already special cases all the other standard corlib interfaces; we should file a work item to have the compiler handle these as well.
API Proposal
It seems the design of CollectionBuilderAttribute
requires a non-generic type; in order to avoid adding a bunch of types we could do the following:
namespace System.Collections.Generic;
public partial class CollectionExtensions
{
[EditorBrowsable(EditorBrowsableState.Never)]
public static LinkedList<T> CreateLinkedList<T>(params ReadOnlySpan<T> values);
[EditorBrowsable(EditorBrowsableState.Never)]
public static Stack<T> CreateStack<T>(params ReadOnlySpan<T> values);
[EditorBrowsable(EditorBrowsableState.Never)]
public static Queue<T> CreateQueue<T>(params ReadOnlySpan<T> values);
}
[CollectionBuilder(typeof(CollectionExtensions), "CreateLinkedList")]
public partial class LinkedList<T>;
[CollectionBuilder(typeof(CollectionExtensions), "CreateStack")]
public partial class Stack<T>;
[CollectionBuilder(typeof(CollectionExtensions), "CreateQueue")]
public partial class Queue<T>;
API Usage
LinkedList<int> values0 = [1, 2, 3];
Stack<int> values1 = [1, 2, 3];
Queue<int> values2 = [1, 2, 3];
Alternative Designs
No response
Risks
No response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment