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
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to set metaobj |
meta | obj | Desired metaobj |
Example
{ 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 # 15obj.meta
obj.meta(self: obj) -> obj|nil
Get the metadata of an object
Arguments
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to get metadata object for |
obj.stringify
obj.stringify(self: obj)
Converts an object into a string representation
Arguments
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to stringify |
pretty | `bool | nil` |
commas | `bool | nil` |
Example
{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
{
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
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to insert |
key | str | Name of the member |
val | any | Value of the member |
Example
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
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to insert |
key | str | Name of the member |
val | any | Value 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
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to get from |
key | str | Name of the member |
Example
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
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to get from |
key | str | Name 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
| Parameter | Type | Description |
|---|---|---|
self | obj | Object 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
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to iterate over |
callback | fun | Callback that takes a key and value |
Example
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
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to iterate over |
callback | fun | Callback that takes a key/value |
Example
{
a = 2.2
b = 4
}.find([](k, v) v.type() == 'f64') # 2.2obj.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
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to iterate over |
callback | fun | Callback that takes a key/value |
Example
{
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
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to check |
key | str | Member 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
| Parameter | Type | Description |
|---|---|---|
self | obj | Array to push item to |
val | any | Any possible value in solus |
Example
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
| Parameter | Type | Description |
|---|---|---|
self | obj | Array to push item to |
val | any | Any possible value in solus |
Example
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
| Parameter | Type | Description |
|---|---|---|
self | obj | Array to pop item from |
Example
Example
val o = { 1, 2, 3, 4, 5 };
for var i = 0; i < o.len(); ++i
io.println(o.pop()); # 5, 4, 3, 2, 1obj.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
| Parameter | Type | Description |
|---|---|---|
self | obj | Array to pop item from |
Example
val o = { 1, 2, 3, 4, 5 };
for var i = 0; i < o.len(); ++i
io.println(o.pop_back()); # 1, 2, 3, 4, 5obj.len
obj.len(obj: obj) -> i64
Returns the total length of the array component of the object
Arguments
| Parameter | Type | Description |
|---|---|---|
self | obj | Array 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
| Parameter | Type | Description |
|---|---|---|
self | obj | Array to remove item from |
index | i64 | Index of the item to remove |
Example
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, 1obj.foreach
obj.foreach(self: obj, callback: fun)
Runs a callback passing each value in the object’s array component
Arguments
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to iterate over |
callback | fun | Callback that takes a value/index |
Example
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
| Parameter | Type | Description |
|---|---|---|
self | obj | Array to reverse order |
Example
{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
| Parameter | Type | Description |
|---|---|---|
self | obj | Array to count items of |
start | i64 | Start index (inclusive) |
end | i64 | End index (inclusive) |
Example
{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
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to iterate over |
callback | fun | Callback that takes a value/index |
Example
{1, nil, nan, 2.2}.where([](v) v.type() == 'f64') # 2.2obj.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
| Parameter | Type | Description |
|---|---|---|
self | obj | Array to search |
callback | fun | Callback that takes a value/index |
Example
{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
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to iterate over |
callback | fun | Callback that takes a value/index |
Example
{ 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
| Parameter | Type | Description |
|---|---|---|
self | obj | Object to check |
match | any | Value to check for |
