Object System
The object system is the core data structure mechanism of the Alia language, supporting flexible data organization and manipulation.
1. Defining Objects
Basic syntax
*[ <Key key>: <Value value>, ... ] ~ object definition
*[ static <Key key>: <Value value>, ... ] ~ static member definition
[ <Value value>, ... ] ~ array definition
Examples
~ Define an object
const person = *[ name: 'Alice', age: 30 ]
~ Define an array
const numbers = [1, 2, 3, 4, 5]
~ Access object properties
print person.name
print person['age']
print numbers[0]
2. Built-in Object Types
| Type | Description |
|---|---|
| Object String | String |
| Object Number | Number |
| Object Boolean | Boolean |
| Object Array | Array |
| Object Object | Object |
| Object Stack | Stack |
| Object Alias | Keyword alias |
| Object Error | Error |
3. Object Operations
Property access
const obj = *[ name: 'Alia', version: 0.1 ]
~ Access a property
print obj.name ~ prints 'Alia'
print obj['version'] ~ prints 0.1
~ Set a property
obj.name = 'Alia'
obj.version = 1.0
Array operations
const arr: [Number, ...] = [1, 2, 3]
~ Access an element
print arr[0] ~ prints 1
~ Modify an element
arr[0] = 10
~ Array length
print arr.length ~ prints 3
4. static Members and self
static members
The static keyword declares an object's static members and static methods. Static members are stored separately from regular members, do not inherit self, and can be accessed directly through the object without instantiation.
const counter = *[
static count: 0
static increment: alias -> {
counter.count = counter.count + 1
counter.count
}
name: 'counter'
]
print counter.count ~ prints 0
print (call counter.increment) ~ prints 1
print (call counter.increment) ~ prints 2
print counter.name ~ prints 'counter'
The self keyword
Inside an object method, self refers to the object itself and can be used to access other members and methods within the object body. self cannot access static members; static methods can neither be accessed with self nor access self.
const obj = *[
value: 42
getValue: alias -> {
self.value
}
]
print (call obj.getValue) ~ prints 42
const obj = *[
static staticValue: 100
value: 200
tryAccessStatic: alias -> {
self.staticValue ~ self cannot access static members; the result is undefined
}
]
print (call obj.tryAccessStatic) ~ prints undefined
5. Object Static Methods
The Object type provides a set of static methods. Multi-argument calls use auxiliary keywords to separate arguments:
const obj = *[ a: 1, b: 2 ]
print Object.keys obj ~ prints a,b
print Object.values obj ~ prints 1,2
print Object.has obj keyed 'a' ~ prints true
print Object.get obj keyed 'a' ~ prints 1
print Object.get obj keyed 'c' or 42 ~ prints 42
Object.set obj keyed 'c' to 3
print Object.get obj keyed 'c' ~ prints 3
print Object.delete obj keyed 'c' ~ prints true
print Object.isObject obj ~ prints true
| Method | Parameters | Description |
|---|---|---|
Object.create | value | Creates an object |
Object.fromEntries | entries | Creates an object from an array of key-value pairs |
Object.assign | target with source... | Merges objects |
Object.keys | obj | Gets the object's array of keys |
Object.values | obj | Gets the object's array of values |
Object.entries | obj | Gets the object's array of key-value pairs |
Object.contact | left with right | Concatenates two objects |
Object.isObject | value | Checks whether the value is an object |
Object.clone | obj | Clones an object |
Object.has | obj keyed key | Checks whether the object contains the specified key |
Object.set | obj keyed key to value | Sets an object property |
Object.get | obj keyed key or fallback | Gets an object property, with a default value |
Object.delete | obj keyed key | Deletes an object property |
const left = *[ x: 1 ]
const right = *[ y: 2 ]
const merged = Object.contact left with right
print merged.x .. merged.y ~ prints 12
const assigned = Object.assign left with right
print assigned.y ~ prints 2
6. Example Code
~ Comprehensive object system example
~ 1. Create an object
const car = *[
brand: 'Tesla',
model: 'Model 3',
year: 2023,
features: ['autopilot', 'touchscreen', 'electric']
]
~ 2. Access object properties
print 'Brand: ' .. car.brand
print 'Model: ' .. car.model
print 'Year: ' .. car.year
~ 3. Iterate over an array
print 'Features:'
for { let i = 0 } i < car.features.length { i ++ } {
print ' - ' .. car.features[i]
}
~ 4. Modify a property
car.year = 2024
print 'Updated year: ' .. car.year
~ 5. Add a new property
Object.set car keyed 'color' to 'white'
print 'Color: ' .. car.color
~ 6. Create a complex object
const company = *[
name: 'Tech Corp',
employees: [
*[ name: 'Alice', role: 'Engineer' ],
*[ name: 'Bob', role: 'Manager' ]
],
departments: *[
engineering: 50,
marketing: 20,
sales: 30
]
]
~ 7. Access nested properties
print 'Company: ' .. company.name
print 'Engineers: ' .. company.departments.engineering
print 'First employee: ' .. company.employees[0].name