-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: allow collection expressions for 'System.Collections.ObjectModel' collection types #110161
Comments
Tagging subscribers to this area: @dotnet/area-system-collections |
SGTM 👍 |
@eiriktsarpalis I just realized it would make sense to also do the same for:
Can I just update this proposal or should I create a new one? |
Feel free to update the current proposal. |
@Sergio0694 Add |
I intentionally didn't add that because it's a wrapper around |
namespace System.Collections.ObjectModel
{
[CollectionBuilder(typeof(ReadOnlyCollection), "CreateCollection")]
public partial class ReadOnlyCollection<T>;
[CollectionBuilder(typeof(ReadOnlyCollection), "CreateSet")]
public partial class ReadOnlySet<T>;
public static class ReadOnlyCollection
{
public static ReadOnlyCollection<T> CreateCollection<T>(params ReadOnlySpan<T> values);
public static ReadOnlySet<T> CreateSet<T>(params ReadOnlySpan<T> values);
}
} |
@Sergio0694 care to submit a PR? |
Sure thing, will do! Thank you for bringing this to API review! 😄 |
Background and motivation
This proposal is about allowing collection expressions to be used to initialize
ReadOnlyCollection<T>
instances.There are two main motivations for this:
ReadOnlyCollection<T>
value from some array expression. Being able to use a collection expression would make such scenarios nicer and less verbose.IEnumerable<int> e = [1, 2, 3]
) results in code that doesn't work at all with AOT. To work around this,ReadOnlyCollection<T>
is a very easy and convenient choice: you can simply replace readonly interfaces for public properties in viewmodels with this, and that will make the code perfectly AOT compatible (because CsWinRT will be able to "see" the concrete type being used). Currently though, you cannot use collection expressions with it, meaning when you change properties to use this type, you'll be forced to switch to more verbose syntax with an object creation expression and a nested collection expression. It would be much nicer to just be able to use a collection expression directly.While we're at it, we can also do the same for
ObservableCollection<T>
andCollection<T>
.I'd be happy to contribute this if it gets approved 🙂
Note
Extracted from #108457 (comment)
API Proposal
Note
The contract of this builder method should make it clear that the returning type is guaranteed to be exactly of each of these types, and not of a derived type. That is necessary to ensure that WinRT marshalling will work correctly, as it needs to "see" the types.
API Usage
Risks
No risk. This enables new syntax that couldn't be used before, and developers using that would not care about the concrete type of the inner list being used. Furthermore,
ReadOnlyCollection<T>
doesn't expose the inner list anyway, so it should not even matter at all in any case.The text was updated successfully, but these errors were encountered: