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 17 times, most recently from 267021c to 1c354ca Compare July 2, 2025 02:21
@effect-bot effect-bot force-pushed the next-minor branch 13 times, most recently from 696d35c to fe54587 Compare July 3, 2025 06:42
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