Hyperlinkv0.8.0-beta.28

Option

Option.orElseResultconsteffect/Option.ts:734
<B>(that: LazyArg<Option<B>>): <A>(
  self: Option<A>
) => Option<Result<B, A>>
<A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<Result<B, A>>

Returns the first available value and marks whether it came from the fallback.

When to use

Use when you need to know whether a present value came from the primary or fallback Option.

Details

  • self is SomeSome(Result.fail(value)) (value from primary)
  • self is None, that() is SomeSome(Result.succeed(value)) (value from fallback)
  • Both NoneNone

Example (Tracking value source)

import { Option } from "effect"

console.log(Option.orElseResult(Option.some("primary"), () => Option.some("fallback")))
// Output: { _id: 'Option', _tag: 'Some', value: { _tag: 'Failure', value: 'primary' } }

console.log(Option.orElseResult(Option.none(), () => Option.some("fallback")))
// Output: { _id: 'Option', _tag: 'Some', value: { _tag: 'Success', value: 'fallback' } }
error handlingorElse
Source effect/Option.ts:7348 lines
export const orElseResult: {
  <B>(that: LazyArg<Option<B>>): <A>(self: Option<A>) => Option<Result<B, A>>
  <A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<Result<B, A>>
} = dual(
  2,
  <A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<Result<B, A>> =>
    isNone(self) ? map(that(), result.succeed) : map(self, result.fail)
)