Skip to main content

Object Class

Object is Alia's built-in object class, used to store collections of key-value pairs. Alia's object system provides rich property operations and methods.

Creating Objects​

Using Literals​

const obj1 = *[name: 'Alice', age: 25]
const obj2 = *[
name: 'Bob',
age: 30,
address: *[
city: 'Beijing',
country: 'China'
]
]

Using the Constructor​

const seed = *[name: 'Alice']
const obj3 = Object.create seed
const entries = [['name', 'Alice'], ['age', 25]]
const obj4 = Object.fromEntries entries

Importing from a JSON File​

Alia supports importing data directly from JSON files via the import statement, automatically converting it to the Object type.

~ Contents of config.json:
~ { "name": "Alice", "age": 30, "isStudent": true, "scores": [95, 88, 92] }

import 'config.json' as config

print Object.get config keyed 'name' ~ outputs 'Alice'
print Object.get config keyed 'age' ~ outputs 30
print Object.get config keyed 'isStudent' ~ outputs true

Features:

  • JSON objects are automatically converted to Alia Objects
  • JSON arrays are automatically converted to Alia Arrays
  • JSON primitive types (strings, numbers, booleans, null) are automatically converted to the corresponding Alia types
  • Nested structures are supported

Methods​

Property operations on Object are performed uniformly through static method calls, with the object passed in as the first argument.

Object.create​

Creates a new object. Takes a single value argument and returns the new object.

const seed = *[name: 'Alice']
const obj = Object.create seed

Object.fromEntries​

Creates an object from an array of key-value pairs. Takes a single array argument and returns the new object.

const entries = [['name', 'Alice'], ['age', 25]]
const obj = Object.fromEntries entries
print obj ~ outputs *[name: Alice, age: 25]

Object.assign​

Merges multiple objects. Source objects are chained after the target with with; the number of sources is variable, and the merged new object is returned.

const obj1 = *[name: 'Alice']
const obj2 = *[age: 25]
const obj3 = *[city: 'Beijing']
const merged = Object.assign obj1 with obj2 with obj3
print merged ~ outputs *[name: Alice, age: 25, city: Beijing]

Object.keys​

Returns the array of property names of the object. Takes a single object argument and returns an array.

const obj = *[name: 'Alice', age: 25]
print Object.keys obj ~ outputs ['name', 'age']

Object.values​

Returns the array of property values of the object. Takes a single object argument and returns an array.

const obj = *[name: 'Alice', age: 25]
print Object.values obj ~ outputs ['Alice', 25]

Object.entries​

Returns the array of key-value pairs of the object. Takes a single object argument and returns an array.

const obj = *[name: 'Alice', age: 25]
print Object.entries obj ~ outputs [['name', 'Alice'], ['age', 25]]

Object.contact​

Merges two objects. The left and right objects are chained with with, and the merged new object is returned.

const obj1 = *[name: 'Alice']
const obj2 = *[age: 25]
const merged = Object.contact obj1 with obj2
print merged ~ outputs *[name: Alice, age: 25]

Object.isObject​

Checks whether a value is an Alia object. Takes a single value argument and returns a boolean.

const obj = *[name: 'Alice']
const arr = [1, 2, 3]
print Object.isObject obj ~ outputs true
print Object.isObject arr ~ outputs false

Object.clone​

Creates a shallow copy of the object. Takes a single object argument and returns the new object.

const original = *[name: 'Alice', age: 25]
const copy = Object.clone original
print copy ~ outputs *[name: Alice, age: 25]

Object.set​

Sets a property on the object. The key is specified with keyed and the value with to; the object passed in is returned as-is.

const obj = *[name: 'Alice']
Object.set obj keyed 'age' to 25
print obj ~ outputs *[name: Alice, age: 25]

Object.get​

Gets a property from the object. The key is specified with keyed and the default value with or; when the default value is omitted it is Null. Returns the property value or the default value.

const obj = *[name: 'Alice']
print Object.get obj keyed 'name' ~ outputs 'Alice'
print Object.get obj keyed 'age' or 0 ~ outputs 0

Object.has​

Checks whether the object contains the specified property. The key is specified with keyed; returns a boolean.

const obj = *[name: 'Alice', age: 25]
print Object.has obj keyed 'name' ~ outputs true
print Object.has obj keyed 'email' ~ outputs false

Object.delete​

Deletes a property from the object. The key is specified with keyed; returns a boolean.

const obj = *[name: 'Alice', age: 25]
Object.delete obj keyed 'age'
print obj ~ outputs *[name: Alice]

Object.toPlain​

Converts to a plain value. Takes a single object argument and returns its plain-value representation.

const obj = *[name: 'Alice', age: 25]
print Object.toPlain obj ~ outputs [object Object]

Object.toArray​

Converts to an array of key-value pairs. Takes a single object argument and returns an array.

const obj = *[name: 'Alice', age: 25]
print Object.toArray obj ~ outputs [['name', 'Alice'], ['age', 25]]

Printing Objects​

Directly outputting an object displays its structural representation:

const obj = *[name: 'Alice', age: 25]
print obj ~ outputs Object *[ String name: "Alice", Number age: 25 ]

Property Access​

Access properties using dot syntax:

const obj = *[name: 'Alice', age: 25]
print obj.name ~ outputs 'Alice'
print obj.age ~ outputs 25

Access nested properties using dot syntax:

const obj = *[
user: *[
profile: *[
name: 'Alice'
]
]
]
print obj.user.profile.name ~ outputs 'Alice'

Example Code​

~ Comprehensive object example

~ 1. Create an object
const person = *[name: 'Alice', age: 25]
print person

~ 2. Property access
print 'Name: ' .. person.name
print 'Age: ' .. person.age

~ 3. Check a property
if Object.has person keyed 'name' {
print 'Object contains the name property'
}

~ 4. Set a property
Object.set person keyed 'city' to 'Beijing'
print person

~ 5. Get a property (with a default value)
const email = Object.get person keyed 'email' or 'unknown'
print 'Email: ' .. email

~ 6. Delete a property
Object.delete person keyed 'city'
print person

~ 7. Get keys, values, and entries
print ('All keys: ' .. (Object.keys person))
print ('All values: ' .. (Object.values person))
print ('All entries: ' .. (Object.entries person))

~ 8. Clone an object
const copy = Object.clone person
print copy

~ 9. Merge objects
const extra = *[job: 'Developer']
const merged = Object.contact person with extra
print merged

~ 10. Use static methods
const entries = [['a', 1], ['b', 2]]
const fromEntries = Object.fromEntries entries
print fromEntries

const source1 = *[x: 1]
const source2 = *[y: 2]
const source3 = *[z: 3]
const assigned = Object.assign source1 with source2 with source3
print assigned

~ 11. Nested objects
const nested = *[
user: *[
profile: *[
name: 'Bob',
avatar: 'avatar.png'
]
]
]
print 'Nested access: ' .. (nested.user.profile.name)

~ 12. Iterate over an object
const config = *[host: 'localhost', port: 8080, debug: true]
const keys = Object.keys config
for { let i = 0 } i < keys.length { i ++ } {
print (keys[i] .. ': ' .. (Object.get config keyed keys[i]))
}

Next Steps​