Skip to main content
Version: 1.6.0

Accessing

If we want to access a field, we use the selection operator "." followed by the field name, like so:

type user = {
login : string,
name : string
};
type account = {
user : user,
id : int,
is_admin : bool
};
const user : user = {login: "al", name: "Alice"};
const alice : account = {user, id: 5, is_admin: true};
const is_alice_admin = alice.is_admin; // == true

Instead of the field name, we can provide between square brackets a string that contains the field name, or an integer that is the index of the field in the record declaration:

We can also access fields of a record using the destructuring syntax, known as pattern matching, which allows accessing multiple fields of a record in parallel, like so:

function userToTuple (a : account) {
const {user, id, is_admin} = a;
return [user, id, is_admin];
}

We can ignore some fields by calling the predefined function ignore on them, like so:

function getId (a : account) {
let {user, id, is_admin} = a;
ignore([user, is_admin]); // To avoid a warning
return id;
}