Skip to content

Commit

Permalink
add indexers and ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
RexJaeschke authored and BillWagner committed Feb 6, 2023
1 parent 124fef4 commit 867ba90
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions standard/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3083,7 +3083,7 @@ An *anonymous_method_expression* is one of two ways of defining an anonymous fun
### 11.8.1 General
The `+`, `-`, `!`, `~`, `++`, `--`, cast, and `await` operators are called the unary operators.
The `+`, `-`, `!`, `~`, `^`, `++`, `--`, cast, and `await` operators are called the unary operators.
```ANTLR
unary_expression
Expand All @@ -3092,6 +3092,7 @@ unary_expression
| '-' unary_expression
| '!' unary_expression
| '~' unary_expression
| '^' unary_expression
| pre_increment_expression
| pre_decrement_expression
| cast_expression
Expand Down Expand Up @@ -3363,7 +3364,10 @@ At run-time, the expression `await t` is evaluated as follows:
An awaiter’s implementation of the interface methods `INotifyCompletion.OnCompleted` and `ICriticalNotifyCompletion.UnsafeOnCompleted` should cause the delegate `r` to be invoked at most once. Otherwise, the behavior of the enclosing async function is undefined.

<<<<<<< HEAD
<<<<<<< HEAD
=======
=======
>>>>>>> 50008cc (add indexers and ranges)
## §range-operator Range operator

This operator provides a succinct syntax for specifying a (possibly empty) element range suitable for use in denoting a slice of an indexable sequence (§indexable-sequence).
Expand All @@ -3381,36 +3385,56 @@ For an operation of the form `s .. e`, binary operator overload resolution ([§1
System.Range operator ..(System.Index start = 0, System.Index end = ^0);
```

<<<<<<< HEAD
The left and right operands denote, respectively, a start and end Index. For this operator, an object of (the immutable struct) type `System.Range` is returned that contains those Indexes. If the left operand is omitted, an Index of `0` is used. If the right operand is omitted, an Index of `^0` is used. As such,

- `s .. e` is transformed by the implementation to `new System.Range(s, e)`.
- `s ..` is transformed by the implementation to `new System.Range(s, ^0)` (or to `System.Range.StartAt(s)` if that method exists, is accessible, and is declared as follows: `public static Range StartAt (Index start);`).
- `.. e` is transformed by the implementation to `new System.Range(0, e)` (or to `System.Range.EndAt(e)` if that method exists, is accessible, and is declared as follows: `public static Range EndAt (Index end);`).
- `..` is transformed by the implementation to `new System.Range(0, ^0)` (or instead to `System.Range.All` if that property exists, is accessible, and is declared as follows: `public static Range All { get; }`).
=======
The left and right operands denote, respectively, a start and end Index. For this operator, an object of (the immutable struct) type `System.Range` is returned that contains those Indexes. If the left operand is omitted, an Index of `0` is used. If the right operand is omitted, an Index of `^0` is used. As such,
- `s .. e` is transformed by the implementation to `new System.Range(s, e)`.
- `s ..` is transformed by the implementation to `new System.Range(s, ^0)` (or to ` System.Range.StartAt(s)` if that method exists, is accessible, and is declared as follows: `public static Range StartAt (Index start);`).
- `.. e` is transformed by the implementation to `new System.Range(0, e)` (or to ` System.Range.EndAt(e)` if that method exists, is accessible, and is declared as follows: `public static Range EndAt (Index end);`).
- `..` is transformed by the implementation to `new System.Range(0, ^0)` (or instead to ` System.Range.All` if that property exists, is accessible, and is declared as follows: `public static Range All { get; }`).
>>>>>>> 50008cc (add indexers and ranges)
> *Note*: While a Range can be created with a start Index greater than the end Index, any attempt to use that Range to denote a slice from an indexable sequence will result in `System.ArgumentOutOfRangeException`. *end note*
Lifted ([§11.4.8](expressions.md#1148-lifted-operators)) forms of the unlifted predefined range operator defined above are also predefined.

> *Example*: The following example uses array and string indexable sequences:
<<<<<<< HEAD
>
=======
>>>>>>> 50008cc (add indexers and ranges)
> ```csharp
> string[] seasons = new string[] { "Summer", "Autumn", "Winter", "Spring" };
> seasons[1..3] // string[2] "Autumn", "Winter"
> seasons[^2..^1] // string[1] "Winter"
> seasons[2..] // string[2] "Winter", "Spring"
> seasons[1..1] // string[0]
<<<<<<< HEAD
>
=======
>
>>>>>>> 50008cc (add indexers and ranges)
> string s2 = "Hello!";
> Index? startN = 1;
> Index? endN = ^2;
> Range? r = startN .. endN;
> s2[r.Value] // OK: "ell"
> ```
<<<<<<< HEAD
>
> *end example*
>>>>>>> 76616f9 (fix formatting)
=======
> *end example*
>>>>>>> 50008cc (add indexers and ranges)
## 11.9 Arithmetic operators
### 11.9.1 General
Expand All @@ -3419,10 +3443,10 @@ The `*`, `/`, `%`, `+`, and `–` operators are called the arithmetic operators.
```ANTLR
multiplicative_expression
: unary_expression
| multiplicative_expression '*' unary_expression
| multiplicative_expression '/' unary_expression
| multiplicative_expression '%' unary_expression
: range_expression
| multiplicative_expression '*' range_expression
| multiplicative_expression '/' range_expression
| multiplicative_expression '%' range_expression
;
additive_expression
Expand Down

0 comments on commit 867ba90

Please sign in to comment.