Hyperlinkv0.8.0-beta.28

Option

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

Returns Some of the fallback value if self is None; otherwise returns self.

When to use

Use when providing a default plain value (not an Option) as fallback

Details

  • Some → returns self unchanged
  • None → calls onNone(), wraps result in Some, and returns it

Example (Providing a fallback value)

import { Option } from "effect"

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

console.log(Option.some("a").pipe(Option.orElseSome(() => "b")))
// Output: { _id: 'Option', _tag: 'Some', value: 'a' }
error handlingorElse
Source effect/Option.ts:6957 lines
export const orElseSome: {
  <B>(onNone: LazyArg<B>): <A>(self: Option<A>) => Option<B | A>
  <A, B>(self: Option<A>, onNone: LazyArg<B>): Option<A | B>
} = dual(
  2,
  <A, B>(self: Option<A>, onNone: LazyArg<B>): Option<A | B> => isNone(self) ? some(onNone()) : self
)