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
| Parameter | Type | Description |
|---|---|---|
object | 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.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
| Parameter | Type | Description |
|---|---|---|
object | obj | Object to get from |
key | str | Name of the member |
Example
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
| Parameter | Type | Description |
|---|---|---|
object | obj | Object to set metaobj |
meta | obj | Desired metaobj |
Example
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
| Parameter | Type | Description |
|---|---|---|
object | obj | Object to get metaobj |
obj.stringify
obj.stringify(object: obj)
Converts an object into a string representation
Parameters
| Parameter | Type | Description |
|---|---|---|
object | obj | Object 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
| Parameter | Type | Description |
|---|---|---|
object | obj | Object to loop 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 + ": " + str(value));
});obj.foreach
obj.foreach(object: obj, callback: fun)
Runs a callback passing each value in the object’s array component
Parameters
| Parameter | Type | Description |
|---|---|---|
object | obj | Object to loop 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.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
