Skip to content

Checklist and open issues for C# range (..) operator #23205

@gafter

Description

@gafter

A Range .. operator is proposed for C# 7.3. This tracks compiler work for that feature.

Proposals:

See also some discussion elsewhere

Compiler

  • Start work on a new feature branch
  • New token ..
  • Fit into operator precedence somewhere
  • Parser for range expressions
  • Built-in operator overloading
    • Range operator ..(int, int)
    • LongRange operator ..(long, long)
    • Requires some well-known APIs be present
  • Permit user-defined operator
    • Accepting and binding the syntax
    • Decide on some underlying special name
  • Overload the user-defined ones when applicable

API questions

  • What is the name of the thing?
  • Closed interval, half-open interval, or start and count?
  • step?
  • Implements IEnumerable pattern (using value types)?
  • Conversions to array, List, IEnumerable, etc?
  • Constructor vs static factory methods
  • Is there a conversion (built-in or otherwise) from an int range to a long range?
  • Were the types ported to mono? Add types supporting C# 8.0 Ranges mono/mono#12020
namespace System
{
    public struct Range
    {
        public Range(int left, int right) ...
    }
    public struct LongRange
    {
        public LongRange(long left, long right) ...
    }
}
  • Supported in Span<T> and ReadonlySpan<T>?
Span<T> x = ...;
Span<T> y = x.Slice([1..0]);

IDE

  • The ForEachToFor refactoring should recognize range syntax (not applicable to new design)

Open Issues

  • Shall we support dropping one or more operands
    • 1..
    • ..10
    • ..
  • Should we have two versions for half-open and closed intervals
    • Rust: .. for half-open and and ... for closed
    • Swift: ..< for half-open and ... for closed
  • Do we want to do something with arrays for a[1..10]?
  • Do we want to do something with pattern-matching?

Metadata

Metadata

Type

No type

Projects

Status

Requires runtime or BCL work

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions