Chapter 9 : Records

Records are like tuples but with named parameters. In other words, they hold a set of key/data pairs. To instantiate a record, you must first declare its type as follows :

type user = {

  id : nat;

  is_admin : bool;

  name : string


And here is how to define an associated record value :

let alice : user =
id = 1n;
is_admin = true;
name = “Alice”


You can access the whole record or get one key in particular :

let alice_is_admin : bool = alice.is_admin


Given a record value, it is a common design pattern to update only a small number of its fields. Instead of copying the fields that are unchanged, LIGO offers a way to only update the fields that are modified.

One way to understand the update of record values is the functional update. The idea is to have an expression whose value is the updated record.

Let us consider defining a function that translates three-dimensional points on a plane.

type point = {x : int; y : int; z : int}
type vector = {dx : int; dy : int}

let origin : point = {x = 0; y = 0; z = 0}

let xy_translate (p, vec : point * vector) : point =
  {p with x = p.x + vec.dx; y = p.y + vec.dy}

⚠️ Note that p has not been changed by the functional update: a nameless new version of it has been created and returned.

Nested records

CameLIGO provide the ability to perform nested updates on records.

For example if you have the following record structure:

type color =
| Green

type preferences = {
  color : color;
  other : int;

type account = {
  id: int;
  preferences: preferences;

You can update the nested record with the following code:

let change_color_preference (account : account) (color : color) : account =  { account with preferences.color = color }

Your mission

1- Refactor the type of coordinates as a record instead of a tuple. Name the parameters x, y and z.

2- Refactor earth_coordinates with the record type.

3- Create a function changecoordz which takes 2 parameters point of type coordinates, zval of type int. That function returns the point patched with its third attribute equal to given zval.

4- Create a new constant called modified_earth_coordinates initialized by calling the function change_coord_z for earth_coordinates with z value to 5.