Hyperlinkv0.8.0-beta.28

Option

Option.getOrElseconsteffect/Option.ts:617
<B>(onNone: LazyArg<B>): <A>(self: Option<A>) => B | A
<A, B>(self: Option<A>, onNone: LazyArg<B>): A | B

Extracts the value from a Some, or evaluates a fallback thunk on None.

When to use

Use when providing a default value for an absent Option

  • Unwrapping with lazy evaluation of the fallback

Details

  • Some → returns the inner value
  • None → calls onNone() and returns its result
  • onNone is only called when needed (lazy)

Example (Unwrapping with a fallback)

import { Option } from "effect"

console.log(Option.some(1).pipe(Option.getOrElse(() => 0)))
// Output: 1

console.log(Option.none().pipe(Option.getOrElse(() => 0)))
// Output: 0
Source effect/Option.ts:6177 lines
export const getOrElse: {
  <B>(onNone: LazyArg<B>): <A>(self: Option<A>) => B | A
  <A, B>(self: Option<A>, onNone: LazyArg<B>): A | B
} = dual(
  2,
  <A, B>(self: Option<A>, onNone: LazyArg<B>): A | B => isNone(self) ? onNone() : self.value
)
Referenced by 7 symbols