Optional

sealed class Optional<T>

A container that either holds exactly one value of type T (Some), or holds none (None), used across the codebase as an explicit, null-safe substitute for a nullable T? wherever null itself could be a meaningful value to store (so that "no value" and "the value happens to be null" cannot be confused), e.g. as the return type of Cache.get/Cache.set (the evicted pair, if any) or Cached.ifValid.

Being a sealed class, it can be exhaustively pattern-matched with a when:

when (val cached = mguCache[mguRequest]) {
is Optional.Some -> cached.value
else -> { /* compute and cache the value */}
}

Use Optional.of to wrap a nullable value (mapping null to None), Optional.some to wrap a known-non-null value, and Optional.none to get the empty instance.

Type Parameters

T

is the type of the contained value, if any

Inheritors

Types

Link copied to clipboard
object Companion
Link copied to clipboard
object None : Optional<Nothing>

The case of Optional holding no value. Always the same, shared singleton instance.

Link copied to clipboard
data class Some<T>(val value: T) : Optional<T>

The case of Optional holding exactly one, non-null value.

Properties

Link copied to clipboard

Whether this Optional holds no value, i.e. whether it is None. Always !isPresent.

Link copied to clipboard
abstract val isPresent: Boolean

Whether this Optional holds a value, i.e. whether it is a Some.

Link copied to clipboard
abstract val value: T?

The contained value, or null if this is None.

Functions

Link copied to clipboard
abstract fun filter(predicate: (T) -> Boolean): Optional<out T>

Keeps the contained value, if any, only if it satisfies predicate; propagates None unchanged.

Link copied to clipboard
abstract fun <R> map(function: (T) -> R): Optional<out R>

Transforms the contained value, if any, via function; propagates None unchanged otherwise.

Link copied to clipboard
abstract fun toSequence(): Sequence<T>

Converts this Optional into a Sequence of zero or one elements.

Link copied to clipboard
abstract override fun toString(): String