mapOr

inline fun <T, R> T?.mapOr(default: R, transform: (T) -> R?): R

Maps this nullable value to a new value using the given transformation function, or returns the default value if this is null or the transformation returns null.

This function is similar to map, but it also allows you to specify a default value to return if the original value is null or if the transformation function returns null.

For example, if you have a nullable User object and you want to get the user's name, or "Guest" if the user is null or their name is null, you can write:

val userName = user.mapOr("Guest") { it.name }

Return

The result of applying the transformation function to this value, or the default value if this is null or the transformation returns null.

Parameters

default

The value to return if this is null or the transformation returns null.

transform

The function to apply to this value if it's not null.