Spec-Zone.ru › Kotlin 2

require

kotlin-stdlib/kotlin/require

В Kotlin: 1.0

inline fun require(value: Boolean)

Выбрасывает IllegalArgumentException, если value равно false.

Примеры

import kotlin.test.*

fun main() { 
   //sampleStart 
   fun getIndices(count: Int): List<Int> {
    require(count >= 0)
    // ...
    return List(count) { it + 1 }
}

try {
    getIndices(-1)
} catch (e: IllegalArgumentException) {
    println(e.message!!) // Failed requirement.
}

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

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

Выбрасывает IllegalArgumentException с результатом вызова lazyMessage, если value равно false.

Примеры

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 }
}

try {
    getIndices(-1)
} catch (e: IllegalArgumentException) {
    println(e.message!!) // Count must be non-negative, was -1
}

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

© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/require.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API