Skip to content

Data

A data declaration defines a tagged union — Effect's Data.TaggedEnum type and Data.taggedEnum constructors under a single name — ready to destructure with match. The rest of Effect's Data module (Data.Class, Data.TaggedClass, Data.Error) works in VibeLang exactly as in Effect.

Declaring tagged unions with data

data Delivery {
  Waiting
  Sent { tracking: string }
  Rejected { reason: string; note?: string }
}
 
const sent = Delivery.Sent({ tracking: "parcel-8" })
 
function describe(delivery: Delivery) {
  return match (delivery) {
    when { _tag: "Waiting" }: "Waiting for dispatch";
    when { _tag: "Sent", let tracking }: `Tracking: ${tracking}`;
    when { _tag: "Rejected", let reason }: `Rejected: ${reason}`;
  }
}
// Run in an effect function:
//   try Console.log(describe(Delivery.Waiting()))
//   try Console.log(describe(sent))
//   try Console.log(describe(Delivery.Rejected({ reason: "address missing" })))
//   try Console.log(Equal.equals(sent, Delivery.Sent({ tracking: "parcel-8" })))
Waiting for dispatch
Tracking: parcel-8
Rejected: address missing
true
  • A fieldless variant is a bare name (Waiting), constructed with Delivery.Waiting().
  • A payload variant lists fields in braces (Sent { tracking: string }), all shallowly readonly in the resulting type.
  • Values compare and hash structurally (Equal.equals).
  • Match variants with object tag patterns (when { _tag: "Sent", let tracking }:), not class patterns (when Delivery.Sent: is EK0702). Missing a variant without default is a compile error (EK4701).

Generic data types

A data declaration accepts up to four unconstrained type parameters (EK2801 for >4, EK2126 for constraints, EK2803 for defaults), including recursive definitions like trees:

data LoadState<A, E> {
  Idle
  Loaded { value: A }
  Failed { error: E }
}
 
const loaded = LoadState.Loaded({ value: ["one", "two"] })
const failed = LoadState.Failed({ error: "offline" })
const idle = LoadState.Idle()
 
function count(state: LoadState<ReadonlyArray<string>, string>) {
  return match (state) {
    when { _tag: "Loaded", let value }: value.length;
    default: 0;
  }
}
// Run in an effect function:
//   try Console.log(try Effect.try(() => JSON.stringify([loaded, failed, idle])))
//   try Console.log(count(loaded), count(idle))
[{"value":["one","two"],"_tag":"Loaded"},{"error":"offline","_tag":"Failed"},{"_tag":"Idle"}]
2 0

What data compiles to

data Delivery { ... } compiles to standard Effect Data.TaggedEnum + Data.taggedEnum, so TypeScript callers get Delivery, Delivery.$is, and Delivery.$match:

Effect
import { Data } from "effect"
 
type Delivery = Data.TaggedEnum<{
  Waiting: {}
  Sent: { tracking: string }
  Rejected: { reason: string; note?: string }
}>
const Delivery = Data.taggedEnum<Delivery>()

Generic declarations emit the corresponding Data.TaggedEnum.WithGenerics helper interface ($LoadState$Definition) alongside the type and constructor object:

Effect
import { Data } from "effect"
 
type LoadState<A, E> = Data.TaggedEnum<{
  Idle: {}
  Loaded: { readonly value: A }
  Failed: { readonly error: E }
}>
interface $LoadState$Definition extends Data.TaggedEnum.WithGenerics<2> {
  readonly taggedEnum: LoadState<this["A"], this["B"]>
}
const LoadState = Data.taggedEnum<$LoadState$Definition>()

Structural equality and Data.Class

In Effect 4, plain objects, arrays, tuples, maps, and sets already support structural Equal.equals without wrappers (treat compared data as immutable):

const first = { route: "inbox", filters: { unread: true } }
const second = { route: "inbox", filters: { unread: true } }
Equal.equals(first, second) // true
Equal.equals(["inbox", 5] as const, ["inbox", 5] as const) // true
Equal.equals([2, 4, 6], [2, 4, 6]) // true
 
function makeRoute(name: string) {
  return { _tag: "Route" as const, name }
}
Equal.equals(makeRoute("inbox"), makeRoute("inbox")) // true
// Run in an effect function: try Console.log(Equal.equals(first, second), Equal.equals(["inbox", 5] as const, ["inbox", 5] as const), Equal.equals([2, 4, 6], [2, 4, 6]), Equal.equals(makeRoute("inbox"), makeRoute("inbox")))
true true true true

Use Data.Class or Data.TaggedClass when you want methods or getters on a structurally equal value class:

class Label extends Data.Class<{ text: string }> {
  get upper() { return this.text.toUpperCase() }
}
 
class Archived extends Data.TaggedClass("Archived")<{
  documentId: string
}> {}
 
const label = new Label({ text: "pending" })
label.upper // "PENDING"
Equal.equals(label, new Label({ text: "pending" })) // true
new Archived({ documentId: "doc-3" })._tag // "Archived"
// Run in an effect function: try Console.log(label.upper, Equal.equals(label, new Label({ text: "pending" })), new Archived({ documentId: "doc-3" })._tag)
PENDING true Archived

Errors and wrapping native causes

Prefer error Name { ... } (which compiles to Data.TaggedError) for domain failures, and keep a cause: unknown field when wrapping native exceptions:

error DocumentDecodeError {
  cause: unknown
}
 
effect function decodeDocument(text: string) {
  return try Effect.try({
    try: () => JSON.parse(text),
    catch: (cause) => new DocumentDecodeError({ cause })
  })
}
// Inferred: any!DocumentDecodeError
// Run in an effect function:
//   {
//     const jsonValue = try decodeDocument("{\"id\":3}")
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
//   try Console.log(decodeDocument("{") catch (error) => `${error._tag} ${error.cause instanceof SyntaxError}`)
{"id":3}
DocumentDecodeError true

See Data declarations for the grammar and diagnostics.

Coming from Effect

data replaces the Data.TaggedEnum + Data.taggedEnum pair, and error replaces Data.TaggedError. Effect's long-form Data.TaggedEnum and Data.Error still compile when needed.


The matching Effect page: effect.website/docs/v4/data-types/data