Improve exception message for invalid steps and document step requirements
#KT-17176
This commit is contained in:
@@ -20,8 +20,8 @@ public open class CharProgression
|
||||
step: Int
|
||||
) : Iterable<Char> {
|
||||
init {
|
||||
if (step == 0) throw kotlin.IllegalArgumentException("Step must be non-zero")
|
||||
else if (step == Int.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must have an inverse")
|
||||
if (step == 0) throw kotlin.IllegalArgumentException("Step must be non-zero.")
|
||||
if (step == Int.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must be greater than Int.MIN_VALUE to avoid overflow on negation.")
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,6 +59,8 @@ public open class CharProgression
|
||||
|
||||
* The progression starts with the [rangeStart] value and goes toward the [rangeEnd] value not excluding it, with the specified [step].
|
||||
* In order to go backwards the [step] must be negative.
|
||||
*
|
||||
* [step] must be greater than `Int.MIN_VALUE` and not equal to zero.
|
||||
*/
|
||||
public fun fromClosedRange(rangeStart: Char, rangeEnd: Char, step: Int): CharProgression = CharProgression(rangeStart, rangeEnd, step)
|
||||
}
|
||||
@@ -75,8 +77,8 @@ public open class IntProgression
|
||||
step: Int
|
||||
) : Iterable<Int> {
|
||||
init {
|
||||
if (step == 0) throw kotlin.IllegalArgumentException("Step must be non-zero")
|
||||
else if (step == Int.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must have an inverse")
|
||||
if (step == 0) throw kotlin.IllegalArgumentException("Step must be non-zero.")
|
||||
if (step == Int.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must be greater than Int.MIN_VALUE to avoid overflow on negation.")
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,6 +116,8 @@ public open class IntProgression
|
||||
|
||||
* The progression starts with the [rangeStart] value and goes toward the [rangeEnd] value not excluding it, with the specified [step].
|
||||
* In order to go backwards the [step] must be negative.
|
||||
*
|
||||
* [step] must be greater than `Int.MIN_VALUE` and not equal to zero.
|
||||
*/
|
||||
public fun fromClosedRange(rangeStart: Int, rangeEnd: Int, step: Int): IntProgression = IntProgression(rangeStart, rangeEnd, step)
|
||||
}
|
||||
@@ -130,8 +134,8 @@ public open class LongProgression
|
||||
step: Long
|
||||
) : Iterable<Long> {
|
||||
init {
|
||||
if (step == 0L) throw kotlin.IllegalArgumentException("Step must be non-zero")
|
||||
else if (step == Long.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must have an inverse")
|
||||
if (step == 0L) throw kotlin.IllegalArgumentException("Step must be non-zero.")
|
||||
if (step == Long.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must be greater than Long.MIN_VALUE to avoid overflow on negation.")
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,6 +173,8 @@ public open class LongProgression
|
||||
|
||||
* The progression starts with the [rangeStart] value and goes toward the [rangeEnd] value not excluding it, with the specified [step].
|
||||
* In order to go backwards the [step] must be negative.
|
||||
*
|
||||
* [step] must be greater than `Long.MIN_VALUE` and not equal to zero.
|
||||
*/
|
||||
public fun fromClosedRange(rangeStart: Long, rangeEnd: Long, step: Long): LongProgression = LongProgression(rangeStart, rangeEnd, step)
|
||||
}
|
||||
|
||||
@@ -35,13 +35,10 @@ class GenerateProgressions(out: PrintWriter) : BuiltInsSourceGenerator(out) {
|
||||
LONG -> "0L"
|
||||
else -> "0"
|
||||
}
|
||||
val checkZero = """if (step == $zero) throw kotlin.IllegalArgumentException("Step must be non-zero")"""
|
||||
val checkZero = """if (step == $zero) throw kotlin.IllegalArgumentException("Step must be non-zero.")"""
|
||||
|
||||
val minValue = when (kind) {
|
||||
LONG -> "Long.MIN_VALUE"
|
||||
else -> "Int.MIN_VALUE"
|
||||
}
|
||||
val checkMin = """else if (step == $minValue) throw kotlin.IllegalArgumentException("Step must have an inverse")"""
|
||||
val stepMinValue = "$incrementType.MIN_VALUE"
|
||||
val checkMin = """if (step == $stepMinValue) throw kotlin.IllegalArgumentException("Step must be greater than $stepMinValue to avoid overflow on negation.")"""
|
||||
|
||||
val hashCode = "=\n" + when (kind) {
|
||||
CHAR ->
|
||||
@@ -103,6 +100,8 @@ public open class $progression
|
||||
|
||||
* The progression starts with the [rangeStart] value and goes toward the [rangeEnd] value not excluding it, with the specified [step].
|
||||
* In order to go backwards the [step] must be negative.
|
||||
*
|
||||
* [step] must be greater than `$stepMinValue` and not equal to zero.
|
||||
*/
|
||||
public fun fromClosedRange(rangeStart: $t, rangeEnd: $t, step: $incrementType): $progression = $progression(rangeStart, rangeEnd, step)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user