typestar

Records in F#

Immutable named bundles with structural equality built in.

// Records are immutable named bundles; with copies and updates.
type Hive = { Name: string; Frames: int; QueenYear: int }

let willow = { Name = "Willow"; Frames = 8; QueenYear = 2024 }
printfn "%A" willow

// The with expression copies, changing only what you name.
let grown = { willow with Frames = 10 }
printfn "%s grew to %d frames" grown.Name grown.Frames
printfn "Original untouched: %d" willow.Frames

// Structural equality comes free.
printfn "Same hive? %b" (willow = { grown with Frames = 8 })

How it works

  1. type Hive = { ... } declares the shape; construction names fields.
  2. { willow with Frames = 10 } copies, changing one field.
  3. = compares by value, not reference.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
506
Tokens
84
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 72 seconds.

Type this snippet

Step 1 of 3 in Types, step 13 of 27 in Language basics.

← Previous Next →

Records in other languages