ifOrNull

inline fun <T> ifOrNull(condition: Boolean, block: () -> T): T?(source)

Checks condition, and if it is true, runs the block and returns its result. Returns null otherwise.

This is a shorthand for if (condition) { block() } else { null }.

Usage

This function is useful when a condition determines whether an action needs to be performed, with its result returned.

// If `shouldSendPopup` is `false`, `sendPopup` will not get called
val popup: Popup? = ifOrNull(shouldSendPopup) {
sendPopup()
}

Since Kotlin

2.5