Records

A record is a composite data type representing an unordered collection of labelled values. Each pair of label and value is called a member.

If you are familiar with other programming languages, you may recognize records as "objects", "dataclasses" or "dictionaries".

Creating records

Records are created using record expressions, listing each member's label together with an expression denoting its value.

// Example: create a record with two members labelled 'name' and 'latinName'.

let animal = {
    name: "Koala",
    latinName: "Phascolarctos cinereus"
};

// We can now access the members using dot notation:
//    animal.name, or
//    animal.latinName
let animalName = animal.name;

Records may be arbitrarily nested:

// Example: create a nested record

let city = {
    name: "Kyoto",
    location: {
        latitude: "35.011665",
        longitude: "135.768326"
    }
};

// Now we can access the inner record using dot nation
let longitude = city.location.longitude;
chevron-rightAdvanced: Punninghashtag

If you are assembling a record from existing variables, you can use a shorthand syntax to let the variables supply both the label and the value of the members. This is known as "punning".

chevron-rightAdvanced: Creating a record from a named typehashtag

Accessing members

Record members are accessed using dot notation. Nullable records can also be used with dot notation: the result of such an expression is also null.

Updating records

Records, like all other values in FlowScript, are immutable: you cannot modify an existing record but only make modified copies of it. This is accomplished using the with keyword followed by a record expression specifying the members to overwrite or add.

The record expression following a with keyword has all the record's members in its variable scope.

chevron-rightAdvanced: JSON compatibilityhashtag

The records values in FlowScript are designed to be a superset of the JSON arrow-up-rightformat. For this reason, arbitrary text literals can be used in place of labels.

The types of records

The type of a record is an image of the record itself, but with each member's value replaced by its type. The type is inferred automatically from the value. This mechanism is known as structural typing.

In the example above, country1 and country2 have the same type and are therefore compatible: they can both be held by the same variable. For a more in-depth treatment on the subtleties of record type compatibility, refer to the Appendix on subtyping rules.

Last updated

Was this helpful?