Skip to content

Object Module

This module provides functions that allow you to interact with objects without using the built in operators, define metadata, and perform operations on members and array items.

obj.usemeta

obj.usemeta(self: 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.

Arguments

ParameterTypeDescription
selfobjObject to set metaobj
metaobjDesired metaobj

Example

solus
{ 15, 0 }.usemeta({
    _add = [](a, b) {
        { a[0] + b[0], a[1] + b[1] }
    }
    _get = [](o, key) {
        if key == 'x'
            o[0]
        if key == 'y'
            o[1]
        obj.getr(o, key) # o.getr calls _get and recurses
    }
}).x # 15

obj.meta

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

Get the metadata of an object

Arguments

ParameterTypeDescription
selfobjObject to get metadata object for

obj.stringify

obj.stringify(self: obj)

Converts an object into a string representation

Arguments

ParameterTypeDescription
selfobjObject to stringify
pretty`boolnil`
commas`boolnil`

Example

solus
{1, 2, 3, 4, 5}.reverse().stringify(false) # { 5, 4, 3, 2, 1 }

obj.template

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

Creates a special member that will call a function on stringification

Example

solus
{
    x = 2
    formatted = obj.template([self]() $"'{{x = {self.x}}}'")
}.stringify(false, true) # "{ x = 2, formatted = '{x = 2}' }"

Map Functions

These functions operate on the map component of the object, the part that contains the named members.

obj.set

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

Sets the object’s member at the specified key.

Arguments

ParameterTypeDescription
selfobjObject 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.setr

obj.setr(self: obj, key: str, val: any)

Sets the object’s "raw" member at the specified key, bypassing _set metadata

Arguments

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

obj.get

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

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

Arguments

ParameterTypeDescription
selfobjObject to get from
keystrName of the member

Example

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

obj.getr

obj.getr(self: obj, key: str) -> any

Gets the object’s "raw" member at the specified key, bypassing _get metadata

Arguments

ParameterTypeDescription
selfobjObject to get from
keystrName of the member

obj.members

obj.members(obj: obj) -> i64

Returns the total amount of named members stored in the map component of the object.

Arguments

ParameterTypeDescription
selfobjObject to count members of

obj.pairs

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

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

Arguments

ParameterTypeDescription
selfobjObject to iterate 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} = {value}'));

obj.find

obj.find(self: obj, callback: fun) -> any

Returns the first member in the object with which the callback returns true or a truthy value.

Arguments

ParameterTypeDescription
selfobjObject to iterate over
callbackfunCallback that takes a key/value

Example

solus
{
    a = 2.2
    b = 4
}.find([](k, v) v.type() == 'f64') # 2.2

obj.match

obj.match(self: obj, callback: fun) -> obj

Returns all members in the object with which the callback returns true or a truthy value.

Arguments

ParameterTypeDescription
selfobjObject to iterate over
callbackfunCallback that takes a key/value

Example

solus
{
    a = 2.2
    b = 4
    c = 3.14
}.match([](k, v) v.type() == 'f64') # { 2.2, 3.14 }

obj.has

obj.has(self: obj, key: str) -> bool

Returns whether the object has been assigned a member with the specified key name. Members must be explicitly deleted with the delete obj -= 'key' operator.

Arguments

ParameterTypeDescription
selfobjObject to check
keystrMember name

Array Functions

These functions operate on the array component of the object, also known as a list or vector.

obj.push

obj.push(self: obj, val: any)

Pushes or inserts a value at the very end of an array

Arguments

ParameterTypeDescription
selfobjArray to push item to
valanyAny possible value in solus

Example

solus
val o = {};
for var i = 0; i < 5; ++i
    o.push(i + 1);
io.println(o.stringify(false)); # { 1, 2, 3, 4, 5 }

obj.push_back

obj.push_back(self: obj, val: any)

Pushes or inserts a value at the very back of an array

Arguments

ParameterTypeDescription
selfobjArray to push item to
valanyAny possible value in solus

Example

solus
val o = {};
for var i = 0; i < 5; ++i
    o.push_back(i + 1);
io.println(o.stringify(false)); # { 5, 4, 3, 2, 1 }

obj.pop

obj.pop(self: obj) -> any

Pops off a value from the very end of an array, removing and returning it. Returns nil if the array is empty.

Arguments

ParameterTypeDescription
selfobjArray to pop item from

Example

Example

solus
val o = { 1, 2, 3, 4, 5 };
for var i = 0; i < o.len(); ++i
    io.println(o.pop()); # 5, 4, 3, 2, 1

obj.pop_back

obj.pop_back(self: obj) -> any

Pops off a value from the very back of an array, removing and returning it. Returns nil if the array is empty.

Arguments

ParameterTypeDescription
selfobjArray to pop item from

Example

solus
val o = { 1, 2, 3, 4, 5 };
for var i = 0; i < o.len(); ++i
    io.println(o.pop_back()); # 1, 2, 3, 4, 5

obj.len

obj.len(obj: obj) -> i64

Returns the total length of the array component of the object

Arguments

ParameterTypeDescription
selfobjArray to count items of

obj.remove

obj.remove(self: obj, ) -> any|err

Removes and returns a value from the array at a specific index, acting like pop. You can combine this with obj.where to match a specific value. Fails if index is out of bounds.

Arguments

ParameterTypeDescription
selfobjArray to remove item from
indexi64Index of the item to remove

Example

solus
val o = { 1, 2, 3, 4, 5 };
o.remove(2);
for var i = 0; i < o.len(); ++i
    io.println(o.pop_back()); # 5, 4, 2, 1

obj.foreach

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

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

Arguments

ParameterTypeDescription
selfobjObject to iterate 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.reverse

obj.reverse(self: obj) -> obj

Returns an array containing the same values as the original array but reversed.

Arguments

ParameterTypeDescription
selfobjArray to reverse order

Example

solus
{1, 2, 3, 4, 5}.reverse().stringify(false) # { 5, 4, 3, 2, 1 }

obj.range

obj.range(self: obj, start: i64, end: i64) -> obj

Returns an inclusive range of values from an object's array component

Arguments

ParameterTypeDescription
selfobjArray to count items of
starti64Start index (inclusive)
endi64End index (inclusive)

Example

solus
{1, 2, 3, 4, 5}.range(1, 3) # { 2, 3, 4 }

obj.where

obj.where(self: obj, callback: fun) -> any

Returns the first value in the array with which the callback returns true or a truthy value.

Arguments

ParameterTypeDescription
selfobjObject to iterate over
callbackfunCallback that takes a value/index

Example

solus
{1, nil, nan, 2.2}.where([](v) v.type() == 'f64') # 2.2

obj.all

obj.all(self: obj, callback: fun) -> obj

Returns all values in the array with which the callback returns true or a truthy value.

Arguments

ParameterTypeDescription
selfobjArray to search
callbackfunCallback that takes a value/index

Example

solus
{3.14, nil, nan, 2.2}.all([](v) v.type() == 'f64') # { 3.14, 2.2 }

obj.map

obj.map(self: obj, callback: fun) -> obj

Builds a map using all values in the array, performing a transformation on each element via callback.

Arguments

ParameterTypeDescription
selfobjObject to iterate over
callbackfunCallback that takes a value/index

Example

solus
{ 1, 2, 3, 4 }.map([](v) v + 1).stringify() # { 2, 3, 4, 5 }

obj.contains

obj.contains(self: obj, match: any) -> bool

Returns whether the array contains the specified value. This function does not currently interact with metadata.

Arguments

ParameterTypeDescription
selfobjObject to check
matchanyValue to check for