onIsNull

inline fun Any?.onIsNull(block: () -> Unit)

Executes the given block only if this object is null.

This inline function provides a concise way to perform an action when a nullable object is null, acting as a syntactic sugar for a standard if (this == null) { ... } check.

Example:

var user: User? = null
user.onIsNull {
println("User is not initialized.")
}
// Output: User is not initialized.

user = User("John")
user.onIsNull {
println("This will not be printed.")
}
// No output

Parameters

block

The lambda function to be executed if the object is null.

See also