make Range in C# 8.0 Enumerable #3004
Replies: 15 comments 4 replies
-
How would you enumerate ranges with open or from-end indices, such as “1..^3” or “..”? |
Beta Was this translation helpful? Give feedback.
-
I think it is easy to avoid this by check the range is valid at compile time |
Beta Was this translation helpful? Give feedback.
-
or just throw exception when the range is invalid, like if you try to iterate 1..^3 then throw an exception to remind you that you cannot iterate a range like this |
Beta Was this translation helpful? Give feedback.
-
@Rinacm Because the team decided to concentrate on slicing as the motivation behind the feature first and explore expanding on it in later iterations. |
Beta Was this translation helpful? Give feedback.
-
See also dotnet/runtime#462 |
Beta Was this translation helpful? Give feedback.
-
Per my comment at dotnet/runtime#462 (comment), I don't think it would make sense to have If we wanted to allow this syntax I'd recommend making If those checks aren't practical for the compiler to emit then we could introduce a helper API in the namespace System.Runtime.CompilerServices
{
public static class RuntimeHelpers
{
// return type is a struct which implements IEnumerable<int>
public static SomeCustomEnumerable MakeIterator(int startInclusive, int endInclusive);
}
} |
Beta Was this translation helpful? Give feedback.
-
I disagree, I would expect the following to work: foreach (var i in 0 .. a.Length)
a[i] = 0; |
Beta Was this translation helpful? Give feedback.
-
I think that there'd be enough of a variance in the opinion of what that does to make it confusing for a substantial percentage of developers. IMO it should be discussed with the conversation about adding clusivity modifiers to the range syntax, and perhaps even require those modifiers in this case for the sake of clarity, e.g.: // assigns each element of the array
foreach (var i in 0 ..< a.Length)
a[i] = 0;
// prints 1, 2, 3
foreach (var i in 1 ..= 3)
Console.WriteLine(i); |
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
Beta Was this translation helpful? Give feedback.
-
I believe the initial confusion will be brief. Developers will get used to it and carve it in their mind that Also, it's easy to write: int Inclusive(int index) => index + 1;
foreach (var i in 1..Inclusive(10)) {
} |
Beta Was this translation helpful? Give feedback.
-
You can take a loot at other languages. For example, Rust: Also I think they actually should implement |
Beta Was this translation helpful? Give feedback.
-
Some History on Rust and their design choice... Despite that, the world seems to have seized on to exclusivity. I'm an example of someone who hates it, but I'm fully aware of it and would rather see exclusivity everywhere than ambiguous variation depending on context. It is a bad idea to use inclusivity in For loops without clear syntax (like Rust chose) to distinguish between inclusive/exclusive. |
Beta Was this translation helpful? Give feedback.
-
With C# 9.0 you can foreach over a range by defining an extension GetEnumerator or installing the RangeForeach nuget package. |
Beta Was this translation helpful? Give feedback.
-
I thought that's the main usage difference between for loop and foreach loop pattern. You use for when you're operating on number ranges, and then foreach if you just want to iterate a collection. How would the optimizer be able to detect and optimize your use case then? |
Beta Was this translation helpful? Give feedback.
-
I thought of it as I found myself writing:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I do not understand why Range didn't inherit from IEnumerable, I think one of the biggest usage of range is iterate it like
and there's no way to do it in C# 8.0
Beta Was this translation helpful? Give feedback.
All reactions