-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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]: Create Generalized Pattern for Collection Builder Types #112990
Comments
Tagging subscribers to this area: @dotnet/area-system-collections |
Wouldn't this need to be a proposal to update the language specification as well? |
How is the [CollectionBuilderFor] type discovered? Is it automatically active if the compiler sees it in the current compilation or in any of the referenced assemblies? |
FYI, |
I agree that discovery/ambiguity is going to be an issue with a design such as this. |
Likely, yes.
I see two options here:
I know there are things to discuss here, but I did want to at least try to focus discussion on this option to see if we can either eliminate it as an option or find a way to implement it that works for our requirements. |
What prevents a software user from declaring the attribute in the appropriate namespace in their own project or assembly? The compiler recognizes all attributes without regard to the containing assembly name. The compiler will see such a hand-declared type. What does it do, how does it know it's hand-declared versus provided by the dotnet/runtime repo? |
Is that so? I didn't know that. I suppose one possible answer would be to add another attribute to decorate the enumerable-returning method. This attribute would accept a type argument that allows you to specify a specific collection builder. Not sure if we would want this to be generic or to just accept a But at this point that's about the only idea I have. |
... Although I have issues with the original example, since if nullable reference types were enabled the compiler would require that the input collection would be non-null. Which is generally good practice anyways, since in almost all cases you should be passing around empty collections in the first place. |
Background and motivation
Currently, as of .NET 9, there is no generic mechanism for defining collection builder types. This results in collection builders being special cased, currently
IEnumerable<T>
=>T[]
andIList<T> => List<T>
. There may be others but these are the two most common scenarios.This mechanism allows for situations like this:
#111715 proposes adding this capability for
IAsyncEnumerable<T>
but there is no mechanim that can be extended in a generic manner. Instead, they must be special typed just like arrays and lists. This proposal is to add a generic mechanism that can be used to decorate any current or future type.API Proposal
I'm not entirely settled on the API design at this point but the idea is that this attribute becomes the primary driver for defining collection builders. This allows for a general, well-known, and documented solution to adding support for collection types moving forward. This should not resolve #111715 directly, but should provide a mechanism for a separate API proposal which implements the solution provided here.
API Usage
This should be a rough approximation of the special casing that List has for IList.
Alternative Designs
We could stay our current course and set up a handler in the runtime/corelib, similar to how IEnumerable is handled by T[] and IList is handled by List. This is a lot of special casing for a feature that is repeated multiple times.
In .NET 10, we gain
CollectionBuilderAttribute
but this would not allow support to be backported to previous TFMs.Risks
None that I'm aware of.
The text was updated successfully, but these errors were encountered: