Skip to content

Commit 3ff3bd7

Browse files
committed
Do not generalize Int/Long/Char as discrete types
1 parent dea84bc commit 3ff3bd7

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

proposals/open-ended-ranges.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ and ending one before the number of elements in it, so to iterate these indices,
1818
For such a common use case, Kotlin standard library provides various shortcut functions, such as
1919
the `indices` extension property available on many data structures returning that `0..(size - 1)` range,
2020
the `lastIndex` extension property returning the last index value, namely `size - 1`, and finally the `until` infix function
21-
allowing to instantiate a range of discrete integral values like `0 until size` which is equivalent to `0..(size - 1)`.
21+
allowing to instantiate a range of integral values like `0 until size` which is equivalent to `0..(size - 1)`.
2222

2323
Despite all of this, due to asymmetry between `..` and `until`, the former is used more often, even in cases where the latter would be more clear.
2424

@@ -34,7 +34,8 @@ on par with the `..` operator and make it very clear that the upper bound is not
3434

3535
Currently, the use cases of the new operator are mostly covered by the `until` function, that corrects the upper bound
3636
returning a closed range of integral values that would be equivalent to an open-ended range.
37-
However, the `until` function is available only for integral or discrete argument types, such as `Int`, `Long`, `Char`,
37+
However, the `until` function is available only for the types where finding the successor to a value is done by adding 1 to that value,
38+
such as `Int`, `Long`, `Char`,
3839
and having the new operator gives a chance for introducing open-ended ranges for those type that didn't have it before.
3940

4041
### Iterating indices of a data structure
@@ -50,7 +51,7 @@ Such intervals are usually chosen as ranges that include their lower bound and e
5051
adjacent ranges neither have a point where they overlap, nor a point between them that is not contained in these ranges.
5152

5253
Even sometimes when the value is already discrete, for example, when it is expressed as a `Double` number,
53-
and it is possible to represent a half-open range with a closed one by adjusting one of its bounds,
54+
and it is possible to emulate a half-open range with a closed one by adjusting one of its bounds,
5455
in practice it is not convenient to work with such ranges:
5556

5657
```kotlin
@@ -204,8 +205,9 @@ Similar to closed ranges, the `OpenEndRange` interface does not specify contract
204205
however its concrete implementations can do that. For example, an open-ended range of double values equal to another such range
205206
when bounds are respectively equal to each other, or to any empty range of doubles, when it is empty itself.
206207

207-
Also, as a consequence of implementing both `OpenEndRange` and `ClosedRange` in concrete range implementations for standard discrete types,
208-
an open-ended range is equal to the closed range with the same `start` value and `endExclusive` equal to `endInclusive + 1`:
208+
Also, as a consequence of implementing both `OpenEndRange` and `ClosedRange`
209+
in concrete range types for the standard integral types and `Char` type,
210+
in these range types an open-ended range is equal to the closed range with the same `start` value and `endExclusive` equal to `endInclusive + 1`:
209211
```kotlin
210212
0..<10 == 0..9 // true
211213
```

0 commit comments

Comments
 (0)