Introduce OpenEndRange and make primitive ranges implement it

#KT-52932
This commit is contained in:
Ilya Gorbunov
2022-06-13 14:48:07 +03:00
committed by Space
parent d54b5f8e85
commit cd9b36b4c3
19 changed files with 257 additions and 16 deletions
+10 -1
View File
@@ -52,9 +52,18 @@ class GenerateRanges(out: PrintWriter) : BuiltInsSourceGenerator(out) {
"""/**
* A range of values of type `$t`.
*/
public class $range(start: $t, endInclusive: $t) : ${t}Progression(start, endInclusive, $increment), ClosedRange<$t> {
@OptIn(ExperimentalStdlibApi::class)
public class $range(start: $t, endInclusive: $t) : ${t}Progression(start, endInclusive, $increment), ClosedRange<$t>, OpenEndRange<$t> {
override val start: $t get() = first
override val endInclusive: $t get() = last
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@Deprecated("Can throw an exception when it's impossible to represent the value with $t type, for example, when the range includes MAX_VALUE. It's recommended to use 'endInclusive' property that doesn't throw.")
override val endExclusive: $t get() {
if (last == $t.MAX_VALUE) error("Cannot return the exclusive upper bound of a range that includes MAX_VALUE.")
return last + 1
}
override fun contains(value: $t): Boolean = first <= value && value <= last
+10 -1
View File
@@ -555,9 +555,18 @@ import kotlin.internal.*
*/
@SinceKotlin("1.5")
@WasExperimental(ExperimentalUnsignedTypes::class)
public class ${elementType}Range(start: $elementType, endInclusive: $elementType) : ${elementType}Progression(start, endInclusive, 1), ClosedRange<${elementType}> {
@OptIn(ExperimentalStdlibApi::class)
public class ${elementType}Range(start: $elementType, endInclusive: $elementType) : ${elementType}Progression(start, endInclusive, 1), ClosedRange<${elementType}>, OpenEndRange<${elementType}> {
override val start: $elementType get() = first
override val endInclusive: $elementType get() = last
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@Deprecated("Can throw an exception when it's impossible to represent the value with $elementType type, for example, when the range includes MAX_VALUE. It's recommended to use 'endInclusive' property that doesn't throw.")
override val endExclusive: $elementType get() {
if (last == $elementType.MAX_VALUE) error("Cannot return the exclusive upper bound of a range that includes MAX_VALUE.")
return last + 1u
}
override fun contains(value: $elementType): Boolean = first <= value && value <= last