Proposal: Generic ranges or 'any-type' ranges #8794
Replies: 13 comments
-
What is the expected set of values for these ranges? It's not clear to me, especially for The string range is the least clear to me. How would the compiler ever be able to figure out what the expected set of values are between "Alice" and "Bob"? The
or a range that has just two Would the type range just return a set of two type instances ( |
Beta Was this translation helpful? Give feedback.
-
The compiler doesn't need to interpret ranges like it doesn't do with A range is just 2 values: start index and end index and nothing more (and this is true also for the current implementation in C# 8.0 beta). Consuming and interpreting ranges lies entirely on the developers (who write programs in C#). So actually BCL and .NET standard contributors do the same now: they add methods to the .NET standard which consume |
Beta Was this translation helpful? Give feedback.
-
Actually it does, it uses it for slicing. |
Beta Was this translation helpful? Give feedback.
-
Partial dup of https://github.com/dotnet/corefx/issues/35420 |
Beta Was this translation helpful? Give feedback.
-
No, it doesn't. The compiler translates the expression |
Beta Was this translation helpful? Give feedback.
-
Isn't this just a pair of objects of the same type? What makes this worth a language feature over just using a type public struct Pair<T>
{
public T First { get; }
public T Second { get; }
} |
Beta Was this translation helpful? Give feedback.
-
In other words, what makes these things a range, over a pair of objects, if you can use them with arbitrary types? |
Beta Was this translation helpful? Give feedback.
-
Only if that method exists, which it doesn't. Using VS2019 and the latest .NET Core 3.0 SDK preview, this function: public int[] M(int[] array) => array[1..2]; Gets compiled into the IL equivalent of this: public int[] M(int[] array)
{
Range range = new Range(1, 2);
int num = range.Start.IsFromEnd ? (array.Length - range.Start.Value) : range.Start.Value;
int num2 = (range.End.IsFromEnd ? (array.Length - range.End.Value) : range.End.Value) - num;
int[] array2 = new int[num2];
Array.Copy(array, num, array2, 0, num2);
return array2;
} |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h I think it has been made this way mostly for compatibility. |
Beta Was this translation helpful? Give feedback.
-
@antrv You're using the definition of "range" that's like this example:
Your usage of "range" is totally fine, but
That's why In my opinion, |
Beta Was this translation helpful? Give feedback.
-
@verelpode I see no reason to restrict this syntax to integer indexed data structure segments in the way you describe it. I'd be glad to be able to use it like this: |
Beta Was this translation helpful? Give feedback.
-
@lostmsu
That's an inclusive-range example but ofcourse it could also be designed to be exclusive of the second value if desired. There are advantages and disadvantages of both ways. I like the idea of being able to write |
Beta Was this translation helpful? Give feedback.
-
I found this very useful. I use similar aproaches when working with dates and other types of number such double with stepping |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Generic ranges or
any-type
rangesSummary
The currently implemented ranges in C# 8.0 beta is limited to the
int
type. However it would be nice to extend the usage of a new range syntax and to have a possibility to construct ranges of any type.Examples:
Possible usage cases
Enumeration for integral-type ranges and for custom-type ranges like for
int
type ranges:Using as a condition in
case
ofswitch
:Compatibility
The compatibility with the existing implementation in C# 8.0 beta should be kept.
Implementations
Option 1. Generic
Index<T>
andRange<T>
types.The compiler will use the existing
Index
andRange
types forint
ranges, and genericIndex<T>
andRange<T>
types for ranges of another type.Index
andRange
will have the implicit conversion operators to and fromIndex<int>
andRange<int>
correspondingly.foreach
statement will support the following extension methods to support integral-type range enumeration:Option 2. Attributes for the compiler to know the
Index
andRange
type for the specific type.CONS: All predefined types in BCL that have meaning to be used with ranges must have this attribute and defined
XxxIndex
andXxxRange
types.Option 3. Extension methods to construct indexes and ranges.
The compiler uses the constructors of the
Index
andRange
structs in the current implementation. This behavior remains the same forint
ranges.The compiler will use one of the following extension method to construct indices (more specialized extension method must have higher priority):
and the following methods of the specific index types to construct
Ranges
:Beta Was this translation helpful? Give feedback.
All reactions