Record types

A record is a value with named fields:

person : {age: Nat, name: Text} person = {age: 99, name: "Bob"} person@name
⧨
"Bob"

Unlike a tuple, each field has a name, so you read and update fields by name rather than by position. You never need to declare a record type before using one.

The above shows reading a field using @. You can set a field with @fieldname.set:

person = {age: 99, name: "Bob"} person@age.set 42
⧨
{age: 42, name: "Bob"}

Or you can set or modify multiple fields with this syntax, which lets you refer to the current value of each field:

use Nat + person = {age: 99, name: "Bob"} person@{age: @age + 1, name: "Bobby McGee"}
⧨
{age: 100, name: "Bobby McGee"}
Unison also has an older, declaration-based record syntax, which is still supported. See Old-style record types.

Syntax at a glance

SyntaxMeaning
{ x: 1, y: 2 }a record literal; fields are stored in name order, not source order
{ x: Nat, y: Text }a record type
{ x: Nat, y: Text | tl }a record type with extra fields
type Point = Point { x: Nat, y: Nat }a constructor with a record payload
{ x: Nat | r }names the trailing row, so a function can talk about the other fields
r@xget a field
(@x)the getter as a function
r@x.set vset a field
(@x.set)the setter as a function
r@{ x: @x + 1 }a record-set block; each @x is the field's value before it is set
r@f1@f2@{ x: @x + 1 }modify a nested sub-record
match r with { x: x, y: y } -> ...pattern match, can bind any subset of fields of r
{ x: x } = ra destructuring bind — no match needed
{ }the empty record

Creating records

A record literal builds a value with the fields you list. Fields are stored sorted by name, so the order you write them in doesn't matter. Records nest to any depth:

{hd: 1, tl: {hd2: 0, tl: "last"}}

A record can also be the payload of a constructor, which is the recommended way to create named record types:

type Person
Person {age: 30, name: "Alice"}
⧨
Person {age: 30, name: "Alice"}

Getting fields

The getter r@x reads a field. It binds tighter than function application, so f r@x is f (r@x), and it chains, so r@a@b reads the field b of the record in field a:

person = {age: 99, name: "Bob"} person@age
⧨
99
recNested = {a: {b: {c: 42}}} recNested@a@b@c
⧨
42

The @ may be spaced on either side — r@x, r@ x and r @ x are all the same getter. Getters can be passed to other functions like any value:

volunteers = [{age: 21, name: "Alice"}, {age: 22, name: "Bob"}] List.map (@name) volunteers
⧨
["Alice", "Bob"]

Updating fields

r@x.set v returns a copy of r with the field x set to v. Unison values are immutable, so the original is left alone:

person = {age: 99, name: "Bob"} person@age.set 30
⧨
{age: 30, name: "Bob"}

Setters can also be passed as functions, for instance:

volunteers = [{age: 21, name: "Alice"}, {age: 22, name: "Bob"}] List.zipWith (@age.set) volunteers [41, 42]
⧨
[{age: 41, name: "Alice"}, {age: 42, name: "Bob"}]

You can also set or modify several fields at once, and you can reference the current values of all fields:

use Nat + r = {f1: 1, f2: 2} r@{f1: @f1 + 10, f2: @f1 + @f2}
⧨
{f1: 11, f2: 3}

Here f2 becomes @f1 + @f2 where @f1 is still 1.

The syntax works when setting or modifying a nested record, too:

use Nat + use Text ++ e = {employee: {name: "A", salary: 1}} e@employee@{name: @name ++ "!", salary: @salary + 100}
⧨
{employee: {name: "A!", salary: 101}}

When setting or modifying fields, you can change the type of the field values:

r = {x: 5} r@{x: Nat.toText @x}
⧨
{x: "5"}

Pattern matching

A record pattern lists the fields it binds. Fields may be listed in any order, and any subset of the input record's fields can be matched. The rest are ignored:

unpackPerson : Person -> (Text, Nat) unpackPerson = cases Person {age: age, name: name} -> (name, age) partialAge : Person -> Nat partialAge = cases Person {age: age} -> age ( unpackPerson (Person {age: 30, name: "Alice"}) , partialAge (Person {age: 30, name: "Alice"}) )
⧨
(("Alice", 30), 30)

A record pattern can be used in a destructuring bind, too:

use Nat + sum3 : {x: Nat, y: Nat, z: Nat} -> Nat sum3 = cases {x: xval, y: yval, z: zval} -> xval + yval + zval sum3 {x: 1, y: 2, z: 3}
⧨
6

This also shows that the value bound can have whatever name you want, it doesn't need to be the same as the field name.

If a function pattern matches on a record, the function can be passed any superset of the fields expected by the match. For instance, this typechecks:

use Nat + sum3 = cases {x: x, y: y, z: z} -> x + y + z sum3 {extra: "ignored", x: 1, y: 2, z: 3}
⧨
6

A field pattern need not be irrefutable, so a literal in a field can leave a later case reachable:

litPattern : { x: Nat } -> Text
litPattern = cases
  { x: 0 } -> "zero"
  _ -> "other"

(litPattern {x : 0}, litPattern {x: 5})

Rows

A record type's trailing row (a "row" refers to a set of fields) can be named, { x: Nat | r }, which lets a function talk about a record's other fields. For instance, look at the type of (@name.set):

setName : {name: Text | r} -> v -> {name: v | r} setName = (@name.set) setName {name: "Alice"} "Alicia"
⧨
{name: "Alicia"}

Notice that the remaining row is left alone, and the name field is replaced (possibly with a different type value).

Generally you don't need to manually add trailing rows. A function which takes row types in strictly negative position is automatically given trailing row variables, so the function works when passed records with any extra fields.