Skip to content

[API Proposal]: Make Range struct a Enumberable #115138

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

Closed
trevisharp opened this issue Apr 29, 2025 · 1 comment
Closed

[API Proposal]: Make Range struct a Enumberable #115138

trevisharp opened this issue Apr 29, 2025 · 1 comment
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

Comments

@trevisharp
Copy link

Background and motivation

Sometimes we want create a range and the function Enumerable.Range can feel a bit verbose. A small example bellow:

foreach (var i in Enumberable.Range(0, n))
{
	SomeFunc(i);
}

For readability reasons, it would be interesting if we could write:

foreach (var i in 0..n)
{
	SomeFunc(i);
}

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:

var data = (0..n).Select(x => x * x);

Instead of:

var data = Enumerable.Range(0, n).Select(x => x * x);

API Proposal

A simplified implementation could look like this:

public struct Range : IEnumerable<int>
{
	public Index Start { get; private set; }
	public Index End { get; private set; }

	public IEnumerator<int> GetEnumerator()
	{
		if (Start.IsFromEnd || End.IsFromEnd)
			throw new Exception("It is not possible iterate from the end of an arbritary range");
		
		for (int i = Start.Value; i < End.Value; i++)
		{
			yield return i;
		}
	}
	
	// ...
}

The main drawback of this proposal, and what might make it undesirable, is the handling of "from end" indices (^n syntax).

API Usage

var data = (0..n).Select(x => x * x);
foreach (var i in 0..100)
{
	
}
var primes = 
	from i in 0..100
	where (2..i).All(k => i % k != 0)
	select i;

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.

@trevisharp trevisharp added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Apr 29, 2025
@ghost ghost added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Apr 29, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 29, 2025
@Clockwork-Muse
Copy link
Contributor

Duplicate of #90861

@Clockwork-Muse Clockwork-Muse marked this as a duplicate of #90861 Apr 29, 2025
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Apr 29, 2025
@github-actions github-actions bot locked and limited conversation to collaborators May 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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
Projects
None yet
Development

No branches or pull requests

2 participants