A record is a value with named fields:
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"}Syntax at a glance
| Syntax | Meaning |
{ 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@x | get a field |
(@x) | the getter as a function |
r@x.set v | set 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 } = r | a 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 Persontype Person
= docs.fundamentals.dataTypes.recordTypes.Person.Person
{age: Nat, name: Text}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⧨99recNested = {a: {b: {c: 42}}}
recNested@a@b@c⧨42The @ 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:
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:
You can also set or modify several fields at once, and you can reference the current values of all fields:
Here f2 becomes @f1 + @f2 where @f1 is still 1.
The syntax works when setting or modifying a nested record, too:
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:
A record pattern can be used in a destructuring bind, too:
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:
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.