Any

Fallback

This function provides a fallback value if the object it's called on is null.

fun <T> T?.fallback(value: T): T =
    this ?: value
  • Parameters: value (the fallback value of type T).

  • Process: Returns this if it is not null; otherwise, returns the provided value.

  • Return: A non-null value of type T.

Example:

val nullableString: String? = null
val result = nullableString.fallback("default value")
println(result)  // Output: "default value"

Is Not Null

This function checks if the object is not null.

fun Any?.isNotNull(): Boolean = !isNull()
  • Return: true if the object is not null, otherwise false.

Example:

val nullableObject: Any? = "Hello"
println(nullableObject.isNotNull())  // Output: true

val nullObject: Any? = null
println(nullObject.isNotNull())  // Output: false

Is Null

This function checks if the object is null.

fun Any?.isNull(): Boolean = this == null
  • Return: true if the object is null, otherwise false.

Example:

val nullableObject: Any? = null
println(nullableObject.isNull())  // Output: true

val nonNullObject: Any? = "Hello"
println(nonNullObject.isNull())  // Output: false

To Selectable

This function converts a list to a list of Selectable objects.

fun <T> List<T>?.toSelectable(): List<Selectable<T>> {
    return this?.map { Selectable(it) } ?: emptyList()
}
  • Return: A list of Selectable objects if the original list is not null; otherwise, an empty list.

Example:

val items: List<String>? = listOf("Item1", "Item2", "Item3")
val selectableItems = items.toSelectable()
selectableItems.forEach { println(it) }

Last updated