Kotlin 2 · Basics and null safety

Validate arguments and state

require checks arguments and check verifies state, both throwing on failure.

fun withdraw(amount: Int) {
    require(amount > 0) { "amount must be positive" }
    check(balance >= amount) { "insufficient funds" }
}

Official Kotlin 2 documentation

More basics and null safety commands

All basics and null safety commands · Kotlin 2 cheatsheet

Was this page helpful?