require

inline fun require(value: Boolean)

Throws an IllegalArgumentException if the value is false.

Samples

import samples.*
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 }
}

assertFailsWith<IllegalArgumentException> { getIndices(-1) }

assertPrints(getIndices(3), "[1, 2, 3]") 
   //sampleEnd
}

inline fun require(value: Boolean, lazyMessage: () -> Any)

Throws an IllegalArgumentException with the result of calling lazyMessage if the value is false.

Samples

import samples.*
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 }
}

assertFailsWith<IllegalArgumentException> { getIndices(-1) }

assertPrints(getIndices(3), "[1, 2, 3]") 
   //sampleEnd
}