Match & Cond

Powerful pattern matching and conditional expressions

Exhaustive pattern matching and functional conditionals without early returns

What are Match & Cond?

Match provides exhaustive pattern matching for Scala-style case expressions, while Cond offers functional conditional evaluation without early returns or if-else chains.

// Pattern matching with Match
type Status = "pending" | "approved" | "rejected" | "cancelled"

const processStatus = (status: Status) =>
  Match(status)
    .caseValue("pending", "Waiting for review")
    .caseValue("approved", "Request accepted")
    .caseValue("rejected", "Request denied")
    .caseValue("cancelled", "Request cancelled")
    .default("Unknown status")

const message = processStatus("approved") // "Request accepted"

Key Features

Exhaustive Matching

Type-safe pattern matching with default case enforcement

No Early Returns

Functional style without breaking out of expressions

Type Inference

Full TypeScript type inference for all branches

Composable

Chain multiple conditions and patterns together

Common Patterns

Match on Values

const status = "pending"
Match(status)
  .when("pending", () => "⏳ Pending")
  .when("success", () => "✓ Success")
  .when("error", () => "✗ Error")
  .default(() => "Unknown")

Cond for Predicates

const value = 5
Cond.of<string>()
  .when(value < 0, "negative")
  .elseWhen(value === 0, "zero")
  .elseWhen(value > 0, "positive")
  .else("unknown")

Built-in Pattern Matching

const option = Option("value")
option.match({
  Some: (value) => `Found: ${value}`,
  None: () => "Not found",
})

const either = Right<string, string>("success")
either.match({
  Right: (value) => `Success: ${value}`,
  Left: (error) => `Error: ${error}`,
})

When to Use Match & Cond

Complex conditional logic with multiple cases

Status codes, state machines, command handlers

Avoiding if-else chains and early returns

Functional programming style, expression-based code

Pattern matching on monadic types

Option, Either, Try, List - built-in match methods