require
Throws an IllegalArgumentException if the value is false.
Since Kotlin
1.1Samples
import kotlin.test.*
fun main() {
//sampleStart
fun getIndices(count: Int): List<Int> {
require(count >= 0) { "Count must be non-negative, was $count" }
// ...
return List(count) { it + 1 }
}
// getIndices(-1) // will fail with IllegalArgumentException
println(getIndices(3)) // [1, 2, 3]
//sampleEnd
}
Throws an IllegalArgumentException with the result of calling lazyMessage if the value is false.
Since Kotlin
1.1Samples
import kotlin.test.*
fun main() {
//sampleStart
fun getIndices(count: Int): List<Int> {
require(count >= 0) { "Count must be non-negative, was $count" }
// ...
return List(count) { it + 1 }
}
// getIndices(-1) // will fail with IllegalArgumentException
println(getIndices(3)) // [1, 2, 3]
//sampleEnd
}
Throws an IllegalArgumentException if the value is false.
Since Kotlin
1.0Samples
import kotlin.test.*
fun main() {
//sampleStart
fun getIndices(count: Int): List<Int> {
require(count >= 0) { "Count must be non-negative, was $count" }
// ...
return List(count) { it + 1 }
}
// getIndices(-1) // will fail with IllegalArgumentException
println(getIndices(3)) // [1, 2, 3]
//sampleEnd
}
Throws an IllegalArgumentException with the result of calling lazyMessage if the value is false.
Since Kotlin
1.0Samples
import kotlin.test.*
fun main() {
//sampleStart
fun getIndices(count: Int): List<Int> {
require(count >= 0) { "Count must be non-negative, was $count" }
// ...
return List(count) { it + 1 }
}
// getIndices(-1) // will fail with IllegalArgumentException
println(getIndices(3)) // [1, 2, 3]
//sampleEnd
}