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;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.
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?