Hyperlinkv0.8.0-beta.28

Option

Option.orElseconsteffect/Option.ts:657
<B>(that: LazyArg<Option<B>>): <A>(self: Option<A>) => Option<B | A>
<A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<A | B>

Returns the fallback Option if self is None; otherwise returns self.

When to use

Use when you need a lazy fallback Option, such as when building priority chains of optional values.

Details

  • Some → returns self unchanged
  • None → evaluates and returns that()
  • that is lazily evaluated

Example (Providing a fallback Option)

import { Option } from "effect"

console.log(Option.none().pipe(Option.orElse(() => Option.some("b"))))
// Output: { _id: 'Option', _tag: 'Some', value: 'b' }

console.log(Option.some("a").pipe(Option.orElse(() => Option.some("b"))))
// Output: { _id: 'Option', _tag: 'Some', value: 'a' }
error handlingorElseSomefirstSomeOf
Source effect/Option.ts:6577 lines
export const orElse: {
  <B>(that: LazyArg<Option<B>>): <A>(self: Option<A>) => Option<B | A>
  <A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<A | B>
} = dual(
  2,
  <A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<A | B> => isNone(self) ? that() : self
)
Referenced by 1 symbols