Coalesce

public protocol Coalesce[Default]

Raw protocol backing the ?? operator.

Implemented by Optional[T] (with Default = T, Output = T) and by Result[T, E] (with Default = T, Output = T). The operand is a thunk so the default expression is only evaluated when needed — this matters when the default has side effects or is expensive to compute.

Examples

let name: String? = .None; name ?? "anonymous" // "anonymous" let cached: String? = .Some("hi"); cached ?? expensiveLookup() // "hi" — expensiveLookup() not called

Associated Types

type Output

Methods

func coalesce(() -> Default) -> Output

Returns the contained value, or the result of default() if absent.

Defined in lang/std/core/coalesce.ks