Core language

Data and pattern matching

Define sums with data, construct variants, and read them with case.

What

Use data to model bounded domains directly in code. The case form reads shape by shape and keeps branching explicit.

Why

Named variants make domain rules obvious and easier to test. You avoid stringly-typed flags and boolean ambiguity.

How

Define variants once, construct values from those constructors, then consume them with case.

let Port := data {
  | Configured : Int
  | Default
};
let port : Port := .Configured(8080);

When

Reach for this chapter when values have distinct outcomes (task states, command result states, protocol messages).

case port of (
| .Configured(value) => value
| .Default => 3000
);

Analogy

Like a JavaScript union with explicit branches, but checked and navigated with one case expression.

Try it

Read the three snippets in order, then continue to Records and arrays.