Skip to content
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

Allow collection expressions for 'System.Collections.ObjectModel' collection types #111093

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

AlexRadch
Copy link
Contributor

Close #110161

Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

1 similar comment
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 5, 2025
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.
Eliminated the `using System.ComponentModel;` directive, reducing dependencies and potentially simplifying the code structure.
/// <returns>A new <see cref="ReadOnlySet{T}"/> containing the specified values.</returns>
[Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0301:Simplify collection initialization", Justification = "This method simplifies ReadOnlySet<T> initialization, so you cannot simplify collection initialization inside it.")]
public static ReadOnlySet<T> CreateSet<T>(params ReadOnlySpan<T> values) =>
values.IsEmpty ? ReadOnlySet<T>.Empty : new ReadOnlySet<T>((HashSet<T>)[.. values]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the spread operator on sets doesn't appear to produce optimal code:

Using Count is not entirely appropriate because the enumeration might contain duplicates, and in some cases, there could be many of them. This would result in unnecessary memory allocation.

Additionally, I think that the compiler is capable of optimizing while (enumerator.MoveNext()) loops effectively.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will replace the loop with a manual, but should I use the Count property?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, I think that the compiler is capable of optimizing while (enumerator.MoveNext()) loops effectively.

The C# compiler shouldn't be emitting use of an enumerator here, just as it doesn't when foreach'ing over a span, but for some reason is. @cston, @RikkiGibson?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will replace the loop with a manual, but should I use the Count property?

Presumably you mean .Length? The input is a ReadOnlySpan.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably you mean .Length? The input is a ReadOnlySpan.

Yes! I mean the Length property. Is it correct to use Length here or not? Sometimes a source may contain many duplicates. This will result in unnecessary memory allocation.

Should I rewrite the loop to a manual for, or will the compiler error be fixed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! I mean the Length property. Is it correct to use Length here or not? Sometimes a source may contain many duplicates. This will result in unnecessary memory allocation.

Good point. The compiler doesn't pre-size the HashSet instances either so we should do the same here.

Should I rewrite the loop to a manual for, or will the compiler error be fixed?

A handwritten factory is fine I think, it doesn't fundamentally change the amount of IL being generated.

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.
/// <typeparam name="T">The type of elements in the collection.</typeparam>
/// <param name="values">The span of values to include in the collection.</param>
/// <returns>A new <see cref="ReadOnlySet{T}"/> containing the specified values.</returns>
public static ReadOnlySet<T> CreateSet<T>(params ReadOnlySpan<T> values) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for this method to be ReadOnlyCollection.CreateSet<T>() instead of ReadOnlySet.Create<T>() in a new static class?
The later is much more user friendly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The builder methods aren't for users. They are for compilers to build collection expressions.
The compiler only requires static method in non-generic type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for this method to be ReadOnlyCollection.CreateSet<T>() instead of ReadOnlySet.Create<T>() in a new static class? The later is much more user friendly.

I could replace ReadOnlyCollection.CreateSet<T>() with the ReadOnlySet.Create<T>() method if that's more correct.

Copy link
Member

@huoyaoyuan huoyaoyuan Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could replace ReadOnlyCollection.CreateSet<T>() with the ReadOnlySet.Create<T>() method if that's more correct.

That's not what's approved in API review. Questions around the names should go back to the original issue.

Copy link
Contributor Author

@AlexRadch AlexRadch Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could replace ReadOnlyCollection.CreateSet<T>() with the ReadOnlySet.Create<T>() method if that's more correct.

That's not what's approved in API review. Questions around the names should go back to the original issue.

It says that methods can be created in one class or separate ones... #110161 (comment)

We could add another type (e.g. System.Collections.ObjectModel.Collection or one non-generic type per collection (like we do in immutable).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Collections community-contribution Indicates that the PR has been added by a community member new-api-needs-documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: allow collection expressions for 'System.Collections.ObjectModel' collection types
6 participants