Optional

data class Optional<T>

This type represents T as not only potentially nullable data, but also as a data which can not be presented. This type will be useful in cases when T is nullable and null as valuable data too in time of data absence should be presented by some third type.

Let's imagine, you have nullable name in some database. In case when name is not nullable everything is clear - null will represent absence of row in the database. In case when name is nullable null will be a little bit dual-meaning, cause this null will say nothing about availability of the row (of course, it is exaggerated example)

See also

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val data: T?
Link copied to clipboard

Extensions

Link copied to clipboard
inline fun <T> Optional<T>.dataOrElse(block: () -> T): T

Returns Optional.data if Optional.dataPresented of this is true, or call block and returns the result of it

Link copied to clipboard
fun <T> Optional<T>.dataOrNull(): T?

Returns Optional.data if Optional.dataPresented of this is true, or null otherwise

Link copied to clipboard
fun <T> Optional<T>.dataOrThrow(throwable: Throwable): T

Returns Optional.data if Optional.dataPresented of this is true, or throw throwable otherwise

Link copied to clipboard
inline fun <T, R> Optional<T>.mapOnAbsent(block: () -> R): R?

Will call block when data presented (Optional.dataPresented == true)

Link copied to clipboard
inline fun <T, R> Optional<T>.mapOnPresented(block: (T) -> R): R?

Will call block when data presented (Optional.dataPresented == true)

Link copied to clipboard
inline fun <T> Optional<T>.onAbsent(block: () -> Unit): Optional<T>

Will call block when data absent (Optional.dataPresented == false)

Link copied to clipboard
inline fun <T> Optional<T>.onPresented(block: (T) -> Unit): Optional<T>

Will call block when data presented (Optional.dataPresented == true)