W3cubDocs

/Kotlin

requireNotNull

inline fun <T : Any> requireNotNull(value: T?): T

Throws an IllegalArgumentException if the value is null. Otherwise returns the not null value.

inline fun <T : Any> requireNotNull(
    value: T?, 
    lazyMessage: () -> Any
): T

Throws an IllegalArgumentException with the result of calling lazyMessage if the value is null. Otherwise returns the not null value.

import kotlin.test.*

fun main(args: Array<String>) {
//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) }

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

© 2010–2017 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/require-not-null.html