Skip to content

Match.tagsTupleExhaustive has been added #4675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: next-minor
Choose a base branch
from

Conversation

KhraksMamtsov
Copy link
Contributor

@KhraksMamtsov KhraksMamtsov commented Mar 31, 2025

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

Match.tagsTupleExhaustive has been added

import { Either, Match, Option } from "effect"

Match.value([Option.some(1), Either.left(33)]).pipe(
  Match.tagsTupleExhaustive({
    NoneLeft: (none, left) => {}, // (none: None<number>, left: Left<number, never>) => void
    NoneRight: (none, right) => {}, // (none: None<number>, left: Right<number, never>) => void
    SomeLeft: (some, left) => {}, // (none: Some<number>, left: Left<number, never>) => void
    SomeRight: (some, right) => {} // (none: Some<number>, left: Right<number, never>) => void
  })
)

It also preserves rendering of union member in type hints.
This is a special sugar for tagged sun-types designed to make it easier to work with a pattern in which the user wants to process all possible combinations of union members in a flat manner.

export const queenBeeState = (swarm: Swarm) => {
  const whiteSurroundedQueenBee = getSurroundedQueenBee(swarm, Side.White);
  const blackSurroundedQueenBee = getSurroundedQueenBee(swarm, Side.Black);

  return Match.value(
    distributive([whiteSurroundedQueenBee, blackSurroundedQueenBee])
  ).pipe(
    Match.when([{ _tag: "Some" }, { _tag: "Some" }], ([white, black]) =>
      QueenBeeState.BothSurrounded({ cells: [white.value, black.value] })
    ),
    Match.when([{ _tag: "None" }, { _tag: "Some" }], ([_white, black]) =>
      QueenBeeState.OneSurrounded({ cell: black.value })
    ),
    Match.when([{ _tag: "Some" }, { _tag: "None" }], ([white, _black]) =>
      QueenBeeState.OneSurrounded({ cell: white.value })
    ),
    Match.when([{ _tag: "None" }, { _tag: "None" }], ([_white, _black]) =>
      QueenBeeState.Free()
    ),
    Match.exhaustive
  );
};

The function is not limited to a pair and can take a tuple of arbitrary length.
image

Related

  • Related Issue #
  • Closes #

Copy link

changeset-bot bot commented Mar 31, 2025

🦋 Changeset detected

Latest commit: 35e0ac3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 32 packages
Name Type
effect Minor
@effect/cli Major
@effect/cluster Major
@effect/experimental Major
@effect/opentelemetry Major
@effect/platform-browser Major
@effect/platform-bun Major
@effect/platform-node-shared Major
@effect/platform-node Major
@effect/platform Major
@effect/printer-ansi Major
@effect/printer Major
@effect/rpc Major
@effect/sql-clickhouse Major
@effect/sql-d1 Major
@effect/sql-drizzle Major
@effect/sql-kysely Major
@effect/sql-libsql Major
@effect/sql-mssql Major
@effect/sql-mysql2 Major
@effect/sql-pg Major
@effect/sql-sqlite-bun Major
@effect/sql-sqlite-do Major
@effect/sql-sqlite-node Major
@effect/sql-sqlite-react-native Major
@effect/sql-sqlite-wasm Major
@effect/sql Major
@effect/typeclass Major
@effect/vitest Major
@effect/ai Major
@effect/ai-anthropic Major
@effect/ai-openai Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@KhraksMamtsov
Copy link
Contributor Author

But maybe would be better to introduce Tagged.ts module with functions for type Tagged = {_tag: string}?
@mikearnaldi what do you think?

@effect-bot effect-bot force-pushed the next-minor branch 12 times, most recently from 0dcc444 to eff836f Compare April 1, 2025 23:11
@effect-bot effect-bot force-pushed the next-minor branch 11 times, most recently from 5a37adc to 9438333 Compare April 9, 2025 02:40
@effect-bot effect-bot force-pushed the next-minor branch 2 times, most recently from 729efa8 to 504de99 Compare May 8, 2025 21:44
@tim-smart
Copy link
Contributor

Probably what really needs changing then is improving the exhaustiveness checks.

@effect-bot effect-bot force-pushed the next-minor branch 4 times, most recently from 65cd12f to a8b59f1 Compare May 9, 2025 17:57
@KhraksMamtsov
Copy link
Contributor Author

@tim-smart
What do you mean? Should we always consider the input data in a distributive way?

@tim-smart
Copy link
Contributor

The match exclusion logic should be updated so this is exhaustive by default.

@effect-bot effect-bot force-pushed the next-minor branch 5 times, most recently from 891e744 to 5a65e24 Compare May 18, 2025 13:39
@effect-bot effect-bot force-pushed the next-minor branch 16 times, most recently from 59ef614 to 7d8c110 Compare May 24, 2025 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Discussion Ongoing
Development

Successfully merging this pull request may close these issues.

2 participants