Records
Records and record types, the operations that select, replace, remove and add fields, scoped labels, and the empty record of each universe.
{ name = "Ada", age = 36 } a record
{ name : Text, age : Natural } its type
r.name a field
r.{ name, age } some of the fields, as a record
r with age = 37 the record with a field replaced
r with address.city = "London" the same for a field of a field
r without age the record without a field
{ email = "ada@example.org", ...r } the record with one more field
r // s the fields of s, and the other fields of r
r /\ s both, merging the fields that are records
A //\\ B the same for record types
{ name } short for { name = name }let ada = { name = "Ada", age = 36, address = { city = "Paris" } }
in { field = ada.name
, fields = ada.{ name, age }
, replaced = ada with age = 37
, nested = ada with address.city = "London"
, removed = ada without address
, extended = { email = "ada@example.org", ...ada }
, preferred = ada // { age = 37, retired = True }
, combined = ada /\ { address = { country = "France" } }
}Normal form
{ combined =
{ address = { city = "Paris", country = "France" }
, age = 36
, name = "Ada"
}
, extended =
{ address = { city = "Paris" }
, age = 36
, email = "ada@example.org"
, name = "Ada"
}
, field = "Ada"
, fields = { age = 36, name = "Ada" }
, nested = { address = { city = "London" }, age = 36, name = "Ada" }
, preferred =
{ address = { city = "Paris" }
, age = 37
, name = "Ada"
, retired = True
}
, removed = { age = 36, name = "Ada" }
, replaced = { address = { city = "Paris" }, age = 37, name = "Ada" }
}The order of the fields does not matter, and a record cannot have the same label twice:
{ a = 1, a = 2 }Error E0008
error[E0008]: the label `a` is given twice
┌─ example.omg:1:10
│
1 │ { a = 1, a = 2 }
│ - first occurrence
│ ^ second occurrence
│
= help: remove one of them, or rename it
/\ requires the fields that both records have to be records themselves:
let r = { a = 1, b = 2 }
in r /\ { a = 3 }Error E0019
error[E0019]: both records have a field `a`
┌─ example.omg:2:5
│
2 │ in r /\ { a = 3 }
│ ^^^^^^^^^^^^^^ these records cannot be combined
│
= `/\` merges two records recursively and requires fields they share to be records themselves
= help: use `//` to let the right-hand record win instead
//\\ merges record types, as /\ merges records:
{ a : Natural } //\\ { b : Text }Normal form
{ a : Natural, b : Text }The fields of a record are all of one kind: values, written with = or as
a bare name, or types, written with :. { ...r } is a record type.
Scoped labels
Extending a record with a field that it already has hides the old field,
and without uncovers it again. Every other operation concerns the visible
field. s.a selects its value; a projection such as s.{ a } would select
that field as a record. with and // replace the visible value,
keeping the fields that it hides:
let s = { a = 10, ...{ a = 1, b = 2 } }
in { visible = s.a
, uncovered = (s without a).a
, replaced = (s with a = 2).a
, kept = ((s with a = 2) without a).a
, preferred = ((s // { a = 3 }) without a).a
}Normal form
{ kept = 1, preferred = 1, replaced = 2, uncovered = 1, visible = 10 }Scoped labels are what lets row polymorphism work without conditions on the labels that a row variable stands for.
The empty record
The record type without fields is unit, also written {}, and {=} is
its only value. It is the type of the results of the actions that only do
something, such as IO/putStr : Text -> IO unit.
unit exists in every universe: unit @l is the record type without
fields of Type l, and these are different types. The second unit in the list has no @;
its level comes from the type expected there:
let done : unit @1 = {=}
let units = [ unit @1, unit ]
in { units, choice = if True then done else {=} }Type
{ choice : unit @1, units : List (Type 1) }