HOOKSCRIPTv0.9.1

RECORDS

In HookScript, records are a static container type that allow you to store multiple values as field-value pairs. To create a record, you first specify your record type and then assign its values using a list or dict. Unlike a dict, a record can only have values for the fields you specify.

var x record<name, age> = ["name", 7]
print(len(x)) // returns 2

var x record<name, age> = { name: "pj", age: 7}
print(len(x)) // returns 2

When your field name contains spaces, you place the field name in quotes:

var x record<"is cool cat", age> = [true, 7]
x["is a cool cat"] // returns true

Retrieving values by field name

To retrieve a value from a record, use square brackets with the field name in quotes (e.g., x["name"]). A null value is returned if the field is not in the record.

x["name"] // returns "pj"

Assigning values by field name

To assign a value to a record by field name, use square brackets with the field name in quotes (e.g., x["name"]). If the field does not exist on the record, an invalid field error will be thrown

x["name"] = "Cleo"

Retrieving values by field index

Records allow you to retrieve values based on their field index. Indexes are zero-based (e.g., [0] references the first field in your record) and can be negative (e.g., [-1] references the last field in your record)

var x record<name, age> = ["PJ", 7]
x[0] // returns "PJ"
x[-1] // returns 7

Assigning values by field index

Records allow you to assign values based on their field index. Indexes are zero-based (e.g., [0] references the first field in your record) and can be negative (e.g., [-1] references the last field in your record)

x[0] = "Cleo"

Record types

Records can hold any type, including other records. You must specify at least one field name.

To assign fields to a record, place the field names between < and > characters with their types:

var x record<name> // the record has one field called `name` that is an `any` type
var x record<name text> /// the record has one field called `name` that is a `text` type
var x record<name text, age int> // the record has two fields called `name` and `age` of types `text` and `int` respectively
var x dict<name, description text> // the record has two fields called `name` and `description` that are both `text` types

When your record contains other records, you use multiple indexes to retrieve and assign values:

var x record<cat record<name text>> = { cat: ["pj"]}
x["cat"]["name"] // returns "pj"

Loading Playground

Built with in Halifax, Nova Scotia by JW