or

fun <T> T?.or(default: T): T

Returns this value if it's not null, or the default value otherwise.

This is a convenience function that provides a more readable way to express the elvis operator (?:).

For example, instead of writing val name = user?.name ?: "Guest", you can write val name = user?.name.or("Guest").

Return

This value if it's not null, or the default value otherwise.

Parameters

default

The value to return if this is null.


fun <T> List<T>?.or(index: Int, default: T): T

Returns the element at the specified index in the list, or the default value if the list is null or the index is out of bounds.

Return

The element at the given index or the default value.

Parameters

index

The index of the element to retrieve.

default

The value to return if the list is null or the index is invalid.