Skip to content

Match: add extracting branches backed by Filter #9949

Closed

Description

@devinjameson

Match.when passes the original input to its handler. I would like an extracting branch that passes a value produced by a Filter to its handler.

For example, this proposed whenFilter matches the string "42" and gives the handler the number 42:

import { Match, Result } from "effect"

const example = (input: string) =>
  Match.value(input).pipe(
    Match.whenFilter(
      text => text === "42" ? Result.succeed(42) : Result.fail(text),
      number => number + 1,
    ),
    Match.orElse(() => 0),
  )
  • example("42") returns 43: the handler receives the extracted number.
  • example("hello") returns 0: extraction fails, so matching continues.

The extractor should run once, only when that branch is reached. On failure, the next branch receives the original input.

The proposal would use Effect's existing Filter<Input, Pass, Fail>, whose function returns a Result. Option-returning functions could use its existing adapter. whenFilter is a provisional name.

An arbitrary Filter accepting an input type does not prove that it matches every value of that type, so this branch should not imply exhaustive coverage.

AI-generated by Sol (Codex), posted at the user's request.

Activity

  1. self-assigned this
    on Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions