Hyperlinkv0.8.0-beta.28

Option

Option.matchconsteffect/Option.ts:423
<B, A, C = B>(options: {
  readonly onNone: LazyArg<B>
  readonly onSome: (a: A) => C
}): (self: Option<A>) => B | C
<A, B, C = B>(
  self: Option<A>,
  options: { readonly onNone: LazyArg<B>; readonly onSome: (a: A) => C }
): B | C

Pattern-matches on an Option, handling both None and Some cases.

When to use

Use when you need to handle both Some and None in one expression and transform an Option into a plain value.

Details

  • If None, calls onNone and returns its result
  • If Some, calls onSome with the value and returns its result
  • Supports the dual API (data-last and data-first)

Example (Matching on an Option)

import { Option } from "effect"

const message = Option.match(Option.some(1), {
  onNone: () => "Option is empty",
  onSome: (value) => `Option has a value: ${value}`
})

console.log(message)
// Output: "Option has a value: 1"
pattern matchinggetOrElse
Source effect/Option.ts:42316 lines
export const match: {
  <B, A, C = B>(options: {
    readonly onNone: LazyArg<B>
    readonly onSome: (a: A) => C
  }): (self: Option<A>) => B | C
  <A, B, C = B>(self: Option<A>, options: {
    readonly onNone: LazyArg<B>
    readonly onSome: (a: A) => C
  }): B | C
} = dual(
  2,
  <A, B, C = B>(self: Option<A>, { onNone, onSome }: {
    readonly onNone: LazyArg<B>
    readonly onSome: (a: A) => C
  }): B | C => isNone(self) ? onNone() : onSome(self.value)
)
Referenced by 12 symbols