Refine the exception type of EmptyRange.random()
Make it the same type as in case of an empty collection, so that `emptyRange.random()` throws the same exception as `emptyRange.toList().random()`. #KT-15539
This commit is contained in:
@@ -58,7 +58,11 @@ public inline fun CharRange.random(): Char {
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public fun IntRange.random(random: Random): Int {
|
||||
return random.nextInt(this)
|
||||
try {
|
||||
return random.nextInt(this)
|
||||
} catch(e: IllegalArgumentException) {
|
||||
throw NoSuchElementException(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,7 +72,11 @@ public fun IntRange.random(random: Random): Int {
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public fun LongRange.random(random: Random): Long {
|
||||
return random.nextLong(this)
|
||||
try {
|
||||
return random.nextLong(this)
|
||||
} catch(e: IllegalArgumentException) {
|
||||
throw NoSuchElementException(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,7 +86,11 @@ public fun LongRange.random(random: Random): Long {
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public fun CharRange.random(random: Random): Char {
|
||||
return random.nextInt(first.toInt(), last.toInt() + 1).toChar()
|
||||
try {
|
||||
return random.nextInt(first.toInt(), last.toInt() + 1).toChar()
|
||||
} catch(e: IllegalArgumentException) {
|
||||
throw NoSuchElementException(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -50,7 +50,11 @@ public inline fun ULongRange.random(): ULong {
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun UIntRange.random(random: Random): UInt {
|
||||
return random.nextUInt(this)
|
||||
try {
|
||||
return random.nextUInt(this)
|
||||
} catch(e: IllegalArgumentException) {
|
||||
throw NoSuchElementException(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,7 +65,11 @@ public fun UIntRange.random(random: Random): UInt {
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun ULongRange.random(random: Random): ULong {
|
||||
return random.nextULong(this)
|
||||
try {
|
||||
return random.nextULong(this)
|
||||
} catch(e: IllegalArgumentException) {
|
||||
throw NoSuchElementException(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user