orDefault

fun <T> T?.orDefault(defaultValue: T): T

Returns the value of this nullable type if it's not null, or a provided default value if it is. This is a concise alternative to the elvis operator (?:) for cases where the default value is computed or retrieved from a function.

Return

The original value if not null, otherwise the result of the defaultValue lambda.

Parameters

defaultValue

A lambda function that provides the default value to return if the original value is null.

Type Parameters

T

The type of the value.