Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

6. Records

An ADT models a choice between shapes. A record models one shape with named fields, the way a Python dataclass or a dictionary with fixed keys does. You declare the field names and their types once, then build values that carry all of them.

type Point = { x: int, y: int }

let here = Point { x = 3, y = 4 }
let moved = { here with x = 10 }

print here.x
print moved.x
print here.x
print here
3
10
3
Point(x=3, y=4)

You construct a record by writing the type’s constructor name, then the fields in braces: Point { x = 3, y = 4 }. The name in front is what tags the value as a Point, which is why the printout reads Point(x=3, y=4). You read a field with a dot, here.x, exactly as in Python.

The line { here with x = 10 } is copy and update. It does not change here. It returns a new record equal to here except for x, which is why the third print here.x still shows 3. The immutability from lesson 1 carries straight over: a record is like a frozen dataclass, so every update hands you a fresh value and the original stays put. The emitted Python makes this literal:

from dataclasses import dataclass
@dataclass(frozen=True, slots=True)
class Point:
    x: int
    y: int
here = Point(3, 4)
_pf_t0 = here
moved = Point(10, _pf_t0.y)

The _pf_t0 line is the compiler holding on to the record being copied, so a base that took work to compute is evaluated once however many fields the update carries over.

Records also match. A case may name a subset of the fields, and { x } shorthand binds the field to a variable of the same name. The shorthand works the other way round as well: when you already have an x and a y, Point { x, y } builds the record from them, just as Point { x = x, y = y } would.

type Point = { x: int, y: int }

let name p =
  match p:
    case Point { x = 0, y = 0 }: "origin"
    case Point { x, y }: "elsewhere"

print (name (Point { x = 0, y = 0 }))
print (name (Point { x = 3, y = 4 }))
origin
elsewhere

When a field itself holds a record, the update sugar nests: { seg with start.x = 99 } reaches through start and rebuilds the path for you, copying the siblings along the way.

Constructors with named fields

In lesson 5 a constructor carried its values by position: Rect 2.0 3.0, and you had to remember which number was the width. A constructor can name its fields instead. Give it a record’s body:

type Shape =
  | Circle { name: str, radius: float }
  | Rect { name: str, width: float, height: float }

let area s =
  match s:
    case Circle { radius }: 3.14 * radius * radius
    case Rect { width, height }: width * height

let door = Rect { name = "door", width = 2.0, height = 3.0 }
let wide = { door with name = "gate" }

print (area door)
print door.name
print wide
6.0
door
Rect(name='gate', width=2.0, height=3.0)

You build and match a named constructor exactly as you do a record, with the constructor’s name in front. A pattern names the fields it needs and ignores the others, so case Circle { radius }: never looks at name.

Look at door.name. Both constructors declare a name of type str, so every Shape has one, and you can read it or update it without a match. radius is different: only a Circle has it, so you reach it inside the case Circle arm, where you know which shape you hold.

Exercise

The hole marked ? is the new balance for a funded account. Run pyfun check: it reports the type the hole expects. Replace ? with 100 so the program prints the three lines below. Notice that opened.balance still shows 0 afterward, because the update produced a new record.

type Account = { name: str, balance: int }

let opened = Account { name = "Ada", balance = 0 }
let funded = { opened with balance = ? }

print funded.name
print funded.balance
print opened.balance

The checker reports:

note: hole `?` has type `int` — or: List.sum ?, Seq.sum ?, String.len ?, ceil ?
 --> 4:38
  |
4 | let funded = { opened with balance = ? }
  |                                      ^
1 unfilled hole

Expected output:

Ada
100
0

Open in the playground

Show solution
type Account = { name: str, balance: int }

let opened = Account { name = "Ada", balance = 0 }
let funded = { opened with balance = 100 }

print funded.name
print funded.balance
print opened.balance

The update builds a fresh Account with the new balance and leaves opened untouched.