Return to homepage

DOSATO

Copyright © 2024 Sebastiaan Heins

1.9 Objects

Object declaration

An object is a collection of key-value pairs.
Objects are created with the make keyword.
Objects have the object type.
Example:

make object person = {
    "name": "John",
    "age": 30,
    "isMarried": false
}

This creates an object with the keys name, age and isMarried.
The values of the keys are "John", 30 and false respectively.
An object expression is a comma separated list of key-value pairs surrounded by curly brackets.
Any value type can be used as a value in an object.
All keys will be converted to strings.

Accessing values

You can access values in an object with the -> operator.
The -> operator is followed by the key of the value you want to access. (either a string or an identifier)
Example:

make object person = {
    "name": "John",
    "age": 30,
    "isMarried": false
}

do say (person->"name") // John
do say (person->age) // 30, can also use an identifier
do say (person->isMarried) // false

Setting values

You can set values in an object with the -> operator.
The -> operator is followed by the key of the value you want to set. (either a string or an identifier)
Example:

make object person = {
    "name": "John",
    "age": 30,
    "isMarried": false
}

set person->"name" = "Jane"
set person->"age" = 25
set person->isMarried = true // can also use an identifier
set person->occupation = "Programmer" // adds a new key

This sets the values of the keys name, age and isMarried to "Jane", 25 and true respectively.
A new key occupation is added with the value "Programmer".

Removing values

You can remove values in an object with the - operator.
The - operator is followed by the key of the value you want to remove. (string form)
Example:

make object person = {
    "name": "John",
    "age": 30,
    "isMarried": false
}

set person -= "age"

This removes the key age from the object.

Nested objects

Objects can be nested inside other objects.
Example:

make object person = {
    "name": "John",
    "age": 30,
    "isMarried": false,
    "address": {
        "street": "123 Main St",
        "city": "Springfield",
        "state": "IL"
    },
    "children": [
        {
            "name": "Emily",
            "age": 5
        },
        {
            "name": "Bob",
            "age": 10
        }
    ]
}

This creates an object with the keys name, age, isMarried, address and children.
The value of the key address is an object with the keys street, city and state.
The value of the key children is an array with 2 objects.