Spec-Zone.ru › Kotlin 2

check

kotlin-stdlib/kotlin/check

Начиная с Kotlin: 1.0

inline fun check(value: Boolean)

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

Примеры

import kotlin.test.*

fun main() { 
   //sampleStart 
   var someState: String? = null
fun getStateValue(): String {
    val state = checkNotNull(someState) { "State must be set beforehand" }
    check(state.isNotEmpty()) { "State must be non-empty" }
    // ...
    return state
}

// getStateValue() // will fail with IllegalStateException

someState = ""
// getStateValue() // will fail with IllegalStateException

someState = "non-empty-state"
println(getStateValue()) // non-empty-state 
   //sampleEnd
}

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

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

Примеры

import kotlin.test.*

fun main() { 
   //sampleStart 
   var someState: String? = null
fun getStateValue(): String {
    val state = checkNotNull(someState) { "State must be set beforehand" }
    check(state.isNotEmpty()) { "State must be non-empty" }
    // ...
    return state
}

// getStateValue() // will fail with IllegalStateException

someState = ""
// getStateValue() // will fail with IllegalStateException

someState = "non-empty-state"
println(getStateValue()) // non-empty-state 
   //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/check.html

Spec-Zone.ru

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