F# records
last modified December 2, 2026
In this article we define and use records.
A record is a type that groups named values, called fields. Like a tuple it holds several values of possibly different types, but each field has a name, so the code says what a value means. Records are immutable by default: a changed record is a new record created from the old one. They have structural equality, which means two records with the same field values are equal, and they work with pattern matching. Records are the most common way to model data in F#.
Defining a record
A record type is declared with type, followed by the fields
and their types in curly brackets. A record value is created with the same
brackets and a value for every field.
type Book = { Title: string; Author: string; Pages: int }
let dune = { Title = "Dune"; Author = "Frank Herbert"; Pages = 412 }
printfn "%s by %s" dune.Title dune.Author
printfn "%d pages" dune.Pages
We define a Book type and create one book.
type Book = { Title: string; Author: string; Pages: int }
Fields are separated with semicolons. Each field has a name and a type; type names start with an uppercase letter, and so do field names by convention.
let dune = { Title = "Dune"; Author = "Frank Herbert"; Pages = 412 }
The record expression does not name the type. The compiler finds it from the field labels. Fields are read with the dot notation.
λ dotnet fsi book.fsx Dune by Frank Herbert 412 pages
Layout and printing
Longer records are usually written one field per line; the semicolons can
then be left out. The %A format prints a record with its field
names.
type City =
{ Name: string
Country: string
Population: int
Coastal: bool }
let lisbon =
{ Country = "Portugal"
Name = "Lisbon"
Coastal = true
Population = 545_000 }
printfn "%A" lisbon
let prague = { Name = "Prague"; Country = "Czechia"; Population = 1_380_000; Coastal = false }
printfn "%s, %s: %d" prague.Name prague.Country prague.Population
We define a city record on several lines.
let lisbon =
{ Country = "Portugal"
Name = "Lisbon"
Coastal = true
Population = 545_000 }
The fields of a record expression can be given in any order. The record still stores and prints them in the order of the type definition.
λ dotnet fsi layout.fsx
{ Name = "Lisbon"
Country = "Portugal"
Population = 545000
Coastal = true }
Prague, Czechia: 1380000
Field labels and type inference
Since a record expression does not name its type, the compiler infers it from the labels. When two record types share field names, the type declared later wins, which can lead to surprising errors.
type Point = { X: int; Y: int }
type Point3 = { X: int; Y: int; Z: int }
let getX r = r.X
printfn "%d" (getX { X = 1; Y = 2 })
Both types have the fields X and Y.
let getX r = r.X
Nothing tells the compiler which record r is, so the label
X resolves to the most recent type with such a field,
Point3. The call then passes a record with only two fields,
and the compilation fails. The fix is an annotation,
let getX (r: Point) = r.X, or a qualified label in the record
expression, { Point.X = 1; Y = 2 }.
λ dotnet fsi labels.fsx labels.fsx(6,20): error FS0764: No assignment given for field 'Z' of type 'FSI_0001.Point3'
Copy and update
A record cannot be changed after it is created. A copy and update expression creates a new record from an existing one, with some fields replaced.
type Account = { Owner: string; Balance: decimal; Active: bool }
let opened = { Owner = "Lucia"; Balance = 0m; Active = true }
let deposited = { opened with Balance = opened.Balance + 250m }
let closed = { deposited with Balance = 0m; Active = false }
printfn "%A" opened
printfn "%A" deposited
printfn "%A" closed
We follow an account through a few changes.
let deposited = { opened with Balance = opened.Balance + 250m }
The with keyword copies all fields of opened
except Balance, which gets a new value. The original record is
unchanged, as the output shows. Several fields can be replaced at once,
separated with semicolons. What the copy shares with the original is
explained in F# copy by value and
reference; records with mutable fields are covered in
F# mutability.
λ dotnet fsi copy_update.fsx
{ Owner = "Lucia"
Balance = 0M
Active = true }
{ Owner = "Lucia"
Balance = 250M
Active = true }
{ Owner = "Lucia"
Balance = 0M
Active = false }
Equality and comparison
Records are compared by their field values. Two separately created records with the same values are equal. Ordering compares the fields one by one, in the order of the type definition.
type Version = { Major: int; Minor: int; Patch: int }
let a = { Major = 2; Minor = 1; Patch = 0 }
let b = { Major = 2; Minor = 1; Patch = 0 }
let c = { Major = 1; Minor = 12; Patch = 3 }
printfn "%b" (a = b)
printfn "%b" (a = c)
printfn "%b" (c < a)
let releases =
[ { Major = 1; Minor = 9; Patch = 4 }
{ Major = 2; Minor = 0; Patch = 1 }
{ Major = 1; Minor = 10; Patch = 0 } ]
let latest = List.max releases
printfn "latest: %d.%d.%d" latest.Major latest.Minor latest.Patch
releases
|> List.sortBy (fun v -> v.Minor)
|> List.iter (fun v -> printfn "%d.%d.%d" v.Major v.Minor v.Patch)
We compare version records.
printfn "%b" (a = b)
a and b are two different objects, but all
their fields are equal, so the records are equal.
let latest = List.max releases
Major is declared first, so it decides the order; the minor
number is used only when the major numbers are the same.
List.sortBy sorts by a chosen field instead. Sorting is
covered in F# sort.
λ dotnet fsi equality.fsx true false true latest: 2.0.1 2.0.1 1.9.4 1.10.0
Record patterns
A record pattern lists some of the fields of a record and binds or tests
their values. It can be used in let, in function parameters,
and in match.
type Order = { Id: int; Customer: string; Total: decimal; Express: bool }
let order = { Id = 101; Customer = "Tomas"; Total = 84.50m; Express = true }
let { Customer = name; Total = total } = order
printfn "%s pays %M" name total
let label { Id = id; Customer = customer } = $"#{id} {customer}"
printfn "%s" (label order)
let shipping o =
match o with
| { Express = true } -> 15.00m
| { Total = t } when t >= 50m -> 0.00m
| _ -> 4.90m
let small = { order with Id = 102; Total = 20m; Express = false }
let large = { small with Id = 103; Total = 120m }
for o in [order; small; large] do
printfn "%s: shipping %M" (label o) (shipping o)
We take orders apart with record patterns.
let { Customer = name; Total = total } = order
The pattern binds the Customer field to name and
the Total field to total. Fields that are not
listed are ignored.
let label { Id = id; Customer = customer } = $"#{id} {customer}"
A record pattern in place of a parameter takes the argument apart right
away. The function still accepts a whole Order.
| { Express = true } -> 15.00m
| { Total = t } when t >= 50m -> 0.00m
In a match, a field can be compared with a constant or bound to a name and tested in a guard.
λ dotnet fsi patterns.fsx Tomas pays 84.50 #101 Tomas #101 Tomas: shipping 15.00 #102 Tomas: shipping 4.90 #103 Tomas: shipping 0.00
Nested and optional fields
A field can hold another record, a list, or an option. An option field marks data that may be missing.
type Address = { Street: string; City: string }
type Person =
{ Name: string
Address: Address
Phone: string option
Languages: string list }
let maria =
{ Name = "Maria"
Address = { Street = "Hlavna 12"; City = "Kosice" }
Phone = None
Languages = ["Slovak"; "German"] }
printfn "%s lives in %s" maria.Name maria.Address.City
let moved =
{ maria with
Address = { maria.Address with City = "Zilina" }
Phone = Some "+421 900 123 456" }
printfn "%A" moved
let contact p = p.Phone |> Option.defaultValue "no phone"
printfn "%s / %s" (contact maria) (contact moved)
A person record holds an address record, an optional phone number, and a list of languages.
printfn "%s lives in %s" maria.Name maria.Address.City
Fields of nested records are reached with a chain of dots.
let moved =
{ maria with
Address = { maria.Address with City = "Zilina" }
Phone = Some "+421 900 123 456" }
To change a nested field, we copy the outer record and set its
Address to an updated copy of the inner record. Options are
covered in F# option type.
λ dotnet fsi nested.fsx
Maria lives in Kosice
{ Name = "Maria"
Address = { Street = "Hlavna 12"
City = "Zilina" }
Phone = Some "+421 900 123 456"
Languages = ["Slovak"; "German"] }
no phone / +421 900 123 456
Members
A record type can have members: properties and methods computed from its
fields. They are declared after the fields with the member
keyword.
type Rect =
{ Width: float
Height: float }
member r.Area = r.Width * r.Height
member r.IsSquare = r.Width = r.Height
member r.Scale(factor: float) =
{ Width = r.Width * factor; Height = r.Height * factor }
static member Square(side: float) = { Width = side; Height = side }
let door = { Width = 0.9; Height = 2.0 }
printfn "area: %.2f" door.Area
printfn "square: %b" door.IsSquare
let bigger = door.Scale(1.5)
printfn "%.2f x %.2f" bigger.Width bigger.Height
let tile = Rect.Square(0.3)
printfn "area: %.2f, square: %b" tile.Area tile.IsSquare
We add two properties, a method, and a static method to a rectangle.
member r.Area = r.Width * r.Height
The name before the dot, here r, refers to the record the
member is called on. A member without parameters is a property; it is
computed each time it is read.
member r.Scale(factor: float) =
{ Width = r.Width * factor; Height = r.Height * factor }
A method takes its parameters in parentheses, like a .NET method, and here returns a new record.
static member Square(side: float) = { Width = side; Height = side }
A static member belongs to the type, not to a value, and is called with the type name. Members are covered in more detail in F# classes.
λ dotnet fsi members.fsx area: 1.80 square: false 1.35 x 3.00 area: 0.09, square: true
Anonymous records
An anonymous record has fields but no declared type. It is written with
{| and |} and is useful for one-off results.
let stats (values: int list) =
{| Count = values.Length
Min = List.min values
Max = List.max values |}
let s = stats [12; 7; 31; 18]
printfn "%d values from %d to %d" s.Count s.Min s.Max
let point = {| X = 3; Y = 4 |}
let point3 = {| point with Z = 5 |}
printfn "%A" point3
printfn "%b" (point = {| X = 3; Y = 4 |})
A function returns an anonymous record instead of a tuple.
let stats (values: int list) =
{| Count = values.Length
Min = List.min values
Max = List.max values |}
The type of the result is {| Count: int; Max: int; Min: int |}.
Compared with a tuple, the caller reads the values by name.
let point3 = {| point with Z = 5 |}
A copy and update expression on an anonymous record can also add new fields. Anonymous records have structural equality like ordinary records.
λ dotnet fsi anonymous.fsx
4 values from 7 to 31
{| X = 3
Y = 4
Z = 5 |}
true
An inventory
In the last example a list of records describes the stock of a small shop. We find items to reorder, compute the stock value, and restock.
type Item =
{ Name: string
Price: decimal
Quantity: int
Minimum: int }
member i.Value = i.Price * decimal i.Quantity
member i.NeedsOrder = i.Quantity < i.Minimum
let stock =
[ { Name = "pencil"; Price = 0.80m; Quantity = 140; Minimum = 50 }
{ Name = "notebook"; Price = 3.20m; Quantity = 12; Minimum = 20 }
{ Name = "eraser"; Price = 0.50m; Quantity = 35; Minimum = 30 }
{ Name = "stapler"; Price = 9.90m; Quantity = 2; Minimum = 5 } ]
let total = stock |> List.sumBy (fun i -> i.Value)
printfn "stock value: %M" total
let toOrder = stock |> List.filter (fun i -> i.NeedsOrder)
for i in toOrder do
printfn "order %s: %d left, minimum %d" i.Name i.Quantity i.Minimum
let restock (item: Item) =
if item.NeedsOrder then { item with Quantity = item.Minimum * 2 }
else item
let restocked = stock |> List.map restock
for i in restocked do
printfn "%-9s %4d %8M" i.Name i.Quantity i.Value
The Item record has two properties computed from its fields.
let restock (item: Item) =
if item.NeedsOrder then { item with Quantity = item.Minimum * 2 }
else item
The parameter needs a type annotation: NeedsOrder is a member,
not a field, and the compiler does not use member names to infer the type
of a record. Restocking does not change the items in the list. It builds a new list in
which the items below their minimum are replaced with updated copies; the
other records are reused as they are.
λ dotnet fsi inventory.fsx stock value: 187.70 order notebook: 12 left, minimum 20 order stapler: 2 left, minimum 5 pencil 140 112.00 notebook 40 128.00 eraser 35 17.50 stapler 10 99.00
Source
F# language reference: Records
F# language reference: Copy and update record expressions
F# language reference: Anonymous records
In this article we have defined record types, created and printed
records, resolved field labels, made changed copies with
with, compared records, taken them apart with patterns,
nested records and optional fields, added members, and used anonymous
records. Records whose fields hold one of several cases are built with
F# discriminated unions, and
records stored as value types are covered in
F# struct types.
Author
List all F# tutorials.