@ossedb/patmat - v1.0.0
    Preparing search index...

    Interface Matcher<T, R>

    A pattern-match in progress. Clauses are tried in order and the first pattern (and guard, if any) that succeeds wins; later clauses are not evaluated. Terminate the chain with Matcher.otherwise (total) or Matcher.exhaustive (throws NonExhaustiveError on no match).

    interface Matcher<T, R> {
        exhaustive(): R;
        otherwise<R2>(handler: (value: T) => R2): R | R2;
        when<R2>(
            predicate: (value: T) => boolean,
            handler: (value: T) => R2,
        ): Matcher<T, R | R2>;
        with<const Pt extends Pattern, R2>(
            pattern: Pt,
            handler: (captures: CapturesOf<Pt>, value: T) => R2,
        ): Matcher<T, R | R2>;
        with<const Pt extends Pattern, R2>(
            pattern: Pt,
            guard: (captures: CapturesOf<Pt>, value: T) => boolean,
            handler: (captures: CapturesOf<Pt>, value: T) => R2,
        ): Matcher<T, R | R2>;
    }

    Type Parameters

    • T
    • R
    Index
    • Terminates the chain strictly: returns the matched result, or throws NonExhaustiveError if no clause matched. Note this is a runtime check; for compile-time exhaustiveness over an ADT use MyAdt.match.

      Returns R

    • Terminates the chain totally: returns the matched result, or the result of handler if no clause matched. handler is not called when a clause already matched.

      Type Parameters

      • R2

      Parameters

      • handler: (value: T) => R2

      Returns R | R2

    • Adds a predicate-only clause, with no pattern or captures.

      Type Parameters

      • R2

      Parameters

      • predicate: (value: T) => boolean
      • handler: (value: T) => R2

      Returns Matcher<T, R | R2>