Object Library (Obj)
Linked with the "obj" type.
Example Usage
#[main]
fn main() {
const o = new {};
assert_eq(Obj.parent(o), self);
assert_eq(o.parent(), self);
}Obj.any(obj: obj) -> bool
Returns true if this object has any data attached to it.
const obj = new { x: 0, y: 0 };
assert(obj.any());Obj.at(obj: obj, index: int) -> (str, unknown)
Field (name, value) on this object at the given index, or null if the index is out of bounds.
const obj = new { x: 0, y: 0 };
assert_eq(obj[1], ("y", 0));Obj.attributes(obj: obj, path: str = null) -> map
Returns a map of attributes, either for this object if the path is null, or for the field/func/obj at the given path.
Obj.children(obj: obj) -> list
Returns a list containing this objects children.
Obj.contains(obj: obj, name: str) -> bool
Return true if this object contains data with the given name.
Obj.create_type(obj: obj, typename: str) -> void
Add a typename reference to the graph, pointing to this object. Programmatic version of #[type("typename")] attribute.
Obj.dbg_graph() -> void
Utility function for dumping the complete graph, helpful for some debugging cases. To dump a specific node, use Std.dbg(..) with the desired object(s).
Obj.dist(obj: obj, other: obj) -> int
Get the distance between two objects (number of edges that separate them).
Obj.empty(obj: obj) -> bool
Returns true if this object doesn't have any data attached to it.
Obj.exists(obj: obj) -> bool
Returns true if this object reference points to an existing object. This is false if the object has been dropped from the document.
Obj.fields(obj: obj) -> list
Returns a list of fields (tuples with name and value each) on this object.
Obj.from_id(id: str) -> obj
Create a new object reference from an ID. Objects in Stof are references just like data.
Obj.from_map(map: map) -> obj
Get the distance between two objects (number of edges that separate them).
Obj.funcs(obj: obj, attributes: str | list | set = null) -> list
Returns a list of functions on this object, optionally filtering by attributes (str, list of str, set of str, tuple of str).
Obj.get(obj: obj, name: str) -> unknown
Get data on this object by name (field value, fn, or data pointer).
Obj.id(obj: obj) -> str
Return the ID of this object.
Obj.insert(obj: obj, path: str, value: unknown) -> void
Either creates or assigns to a field, just like a normal field assignment, using this object as a starting context.
Obj.instance_of(obj: obj, proto: str | obj) -> bool
Returns true if this object is an instance of a prototype.
Obj.is_parent(obj: obj, other: obj) -> bool
Returns true if this object is a parent of another.
Obj.is_root(obj: obj) -> bool
Returns true if this object is a root.
Obj.len(obj: obj) -> int
Number of fields on this object.
Obj.move(obj: obj, dest: obj) -> bool
Move this object to a new parent destination. Parent destination cannot be a child of this object (node detachment).
Obj.move_field(obj: obj, source: str, dest: str) -> bool
Move or rename a field from a source path/name to a destination path/name (like "mv" in bash), returning true if successfully moved.
Obj.name(obj: obj) -> str
Return the name of this object.
Obj.parent(obj: obj) -> obj
Return the parent of this object, or null if this object is a root.
Obj.path(obj: obj) -> str
Return the path of this object as a dot '.' separated string.
Obj.prototype(obj: obj) -> obj
Returns the prototype object for this object or null if this object doesn't have one.
Obj.remove(obj: obj, path: str, shallow: bool = false) -> bool
Performs a "drop" operation, just like the Std.drop(..) function, using this object as a starting context. Use this to remove fields, functions, data, etc.
Shallow
If shallow is true and the path references an object field, drop the field, but don't drop the object from the graph. Default behavior is to drop objects.
Obj.remove_prototype(obj: obj) -> void
Remove an object's prototype.
Obj.root(obj: obj) -> obj
Returns the root object that contains this object (or self if this object is a root).
Obj.run(obj: obj) -> void
Run an object (like calling a function, but for the entire object as a task). This will execute all fields and functions with a #[run] attribute, optionally with an order #[run(3)]. Any sub objects encountered will also get ran recursively. Arrays act like pipelines, unlocking serious functionality.
Motivation
This concept enables data-driven abstractions above function calls. An example would be setting some fields on an object that already has some #[run] functions defined, ready to utilize the values in those fields. With prototypes, you can probably see how this is a powerful tool.
Concrete Example
Obj.schemafy(schema: obj, target: obj, remove_invalid: bool = false, remove_undefined: bool = false) -> bool
Applies all #[schema] fields from a schema object onto a target object, manipulating the target's fields accordingly and returning true if the target is determined to be valid (matches the schema).
Use Cases
filtering & renaming fields as a batch
validation
structured transformations (to/from APIs, etc.)
access control
Obj.set_prototype(obj: obj, proto: obj | str) -> void
Set the prototype of this object.
Obj.to_map(obj: obj) -> map
Create a new map out of this object's fields.
Obj.upcast(obj: obj) -> bool
Set the prototype of this object to the prototype of this objects existing prototype.
Last updated