-
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! 😄 |
…jectModel' collection types dotnet#110161
…lection types (#111093) * #110161 Allow collection expressions for 'System.Collections.ObjectModel' collection types #110161 * Code review Enhance ReadOnlyCollection with documentation and updates The `EditorBrowsable` attribute is replaced with a `SuppressMessage` attribute to clarify the intended usage of these methods. Additionally, the condition for checking if input values are empty is updated from `values.Length <= 0` to `values.IsEmpty` for improved readability and performance. * Remove unused namespace from ReadOnlyCollection.cs Eliminated the `using System.ComponentModel;` directive, reducing dependencies and potentially simplifying the code structure. * Remove suppression attributes from collection methods The `CreateCollection<T>` method in the `ReadOnlyCollection` class had its suppression attribute removed, addressing a style warning related to collection initialization while maintaining its functionality. Similarly, the `CreateSet<T>` method in the `ReadOnlySet` class also had its suppression attribute removed, preserving its original functionality. * Refactor `(HashSet<T>)[.. values]` to manual loop * Marked CreateCollection and CreateSet methods in ReadOnlyCollection with the `[EditorBrowsable(EditorBrowsableState.Never)]` attribute to hide them from IntelliSense. * Apply suggestions from code review * Refactor ReadOnlyCollection and ReadOnlySet APIs Updated method names in ReadOnlyCollection and ReadOnlySet from CreateCollection and CreateSet to Create for clarity and consistency. Removed the CreateSet method from ReadOnlyCollection to streamline functionality. Adjusted documentation comments for improved clarity. Updated CollectionBuilder attributes to reflect the new method names, ensuring proper linkage for collection creation. * Updated the `CollectionBuilder` attribute to reference `ReadOnlySet` instead of `ReadOnlyCollection`, reflecting a change in the collection type used in the `ReadOnlySet` class. * Code Style * Rename methods in ReadOnlyCollection and ReadOnlySet Updated `ReadOnlyCollection` to rename `Create` to `CreateCollection` and introduced `CreateSet` for creating `ReadOnlySet` instances from spans. Adjusted `CollectionBuilder` attributes accordingly and removed the static `ReadOnlySet` class, integrating its functionality into the main class. Updated the `ReadOnlyCollection` partial class in `System.Runtime.cs` to maintain API consistency. --------- Co-authored-by: Eirik Tsarpalis <[email protected]>
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: