Skip to content

Object Module

This module provides functions that allow you to interact with objects without using the built in operators and construction methods. This is useful for non-identifier friendly keys.

obj.new

obj.new() -> obj

Constructs a new object with no members

obj.set

obj.set(obj: object, key: str, val: any)

Sets the object’s member at the specified key. Not all keys set this way can be accessed with the postfix obj.member operator

Parameters

ParameterTypeDescription
objectobjObject to insert
keystrName of the member
valanyValue of the member

Example

solus
var o = {};
obj.set(o, "wow", 1);
io.println(o.wow); # 1
obj.set(o, "Can't do this with . lol", 2);

obj.get

obj.get(obj: object, key: str) -> any

Gets the object’s member at the specified key. Useful for non identifier friendly member keys

Parameters

ParameterTypeDescription
objectobjObject to get from
keystrName of the member

Example

solus
val x = obj.get(o, "Can with this :)");

obj.usemeta

obj.usemeta(object: obj, meta: obj)

Set the metaobj of an object. This defines custom behavior on the object. Note that if you update the metafuns on the metaobj, you must use it again.

Parameters

ParameterTypeDescription
objectobjObject to set metaobj
metaobjDesired metaobj

Example

solus
var og = { x = 2, y = 2 };
var meta = {
    storage = {}
    _str = [og]() { "{" + str(og.x) + ", " + str(og.y) + "}" }
    _set = [](key, value) { storage[key] = value; }
    _get = [](key) { storage[key] }
    _call = [og]() { str(og) }
};
obj.usemeta(og, meta);
io.println(og()); # {2, 2}

obj.meta

obj.meta(object: obj) -> obj|nil

Get the metaobj of an object

Parameters

ParameterTypeDescription
objectobjObject to get metaobj

obj.stringify

obj.stringify(object: obj)

Converts an object into a string representation

Parameters

ParameterTypeDescription
objectobjObject to stringify

obj.pairs

obj.pairs(object: obj, callback: fun)

Runs a callback passing each key/value pair in the object’s members component

Parameters

ParameterTypeDescription
objectobjObject to loop over
callbackfunCallback that takes a key and value

Example

solus
var o = {
    a = 1
    b = 2
    c = 3
};
obj.pairs(o, [](key, value) {
    io.println(key + ": " + str(value));
});

obj.foreach

obj.foreach(object: obj, callback: fun)

Runs a callback passing each value in the object’s array component

Parameters

ParameterTypeDescription
objectobjObject to loop over
callbackfunCallback that takes a value/index

Example

solus
var o = { 1, 2, 3, 4, 5 };
obj.foreach(o, [](value, i) {
    io.println(value);
});

obj.members

obj.members(obj: obj) -> i64

Returns the amount of values stored in the members component of the object

obj.len

obj.len(obj: obj) -> i64

Returns the total length of the array component of the object

obj.template

obj.template(fmt: fun) -> usr:template

Creates a special member that will call a function on stringification