-
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]: Annotate TSource
with allows ref struct
for both sync/async LINQ
#111830
Comments
As mentioned in #111685 changing this will no doubt require changes to several other parts incl |
Related to #108131 It's currently difficult to implement for many operators. |
I don't believe this would be feasible in practice. Many Linq methods wouldn't be able to support the constraint in principle, resulting in inconsistency between the various methods. Those that could support it currently apply performance optimizations using runtime type checks: runtime/src/libraries/System.Linq/src/System/Linq/Select.cs Lines 26 to 49 in a40f92e
I don't think it would be feasible to keep those while adding the The constraint was added to |
Naturally, |
You currently can't express such tests, eg |
I think we can close this as not planned, for now. We can revisit assuming future versions of the language permit more versatility. |
Background and motivation
Support
IEnumerable<TSource>
/IAsyncEnumerable<TSource>
whereTSource
is aref struct
in LINQ methods where appropriate/possible to allow using these in such cases. E.g. there are cases whereTSource
points to internal state of the enumerable source and one may want to preventToArray
/ToList
but would still like to support transformations likeSelect
or reductions likeCount
or similar.In general, all LINQ methods that do not involve storing/caching
TSource
on the heap should annotated withallows ref struct
.A concrete example of such a case is the .NET CSV library https://github.com/nietras/Sep consult README there for more background if necessary.
Currently, the lack of
allows ref struct
prevents LINQ usage in such cases which either means no support or having to reinvent the wheel for this, which also means a less than ideal user experience since standard and known method cannot be used.cc: @stephentoub
API Proposal
Example API changes, I do not have time to list all possible methods and this can be expanded over time incl. by others.
Above support likely involves annotating support types like:
API Usage
No new API usage as such just expanded support via annotation.
Alternative Designs
None.
Risks
Given
allows ref struct
defines a contract, forward compatibility may be a risk if one later decides to rewrite a given LINQ method in a way that is incompatible with this support. Risk should be very low and considered on a method by method basis.The text was updated successfully, but these errors were encountered: