Optional
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 */}
}Content copied to clipboard
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