[API Proposal]: Make Range struct a Enumberable #115138
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
needs-area-label
An area label is needed to ensure this gets routed to the appropriate area owners
Background and motivation
Sometimes we want create a range and the function Enumerable.Range can feel a bit verbose. A small example bellow:
For readability reasons, it would be interesting if we could write:
This would be much cleaner, and could probably be added naturally to .NET considering that the a..b syntax already creates a Range object. If the type Range implements a IEnumerable we could write other interessting simplifications like:
Instead of:
API Proposal
A simplified implementation could look like this:
The main drawback of this proposal, and what might make it undesirable, is the handling of "from end" indices (^n syntax).
API Usage
Alternative Designs
No response
Risks
The implementation would need to be studied and discussed carefully, especially regarding how to deal with "from end" indices. It still seems a bit far from ideal. This proposal may make things a bit confusing in advanced contexts, but I believe a good discussion can be had here.
I also consider a type of implementation that can cause incorrect behavior in some types that override the '[]' operator for both IEnumerable and Range types. While rare today, this kind of pattern could become more common, inspired by libraries like pandas in Python.
The text was updated successfully, but these errors were encountered: