githubEdit

Standard Library (Std)

Library for standard operations (print, assert, etc.).

Standard Library (Std)

Functions in the 'Std' library are foundational to Stof and therefore do not require one to explicitly reference 'Std' when calling them. Within the standard library, you'll find functions for asserting values, printing to the console, throwing errors, putting processes to sleep, etc. Note for advanced users that it is possible to extend or modify this library as needed.

Example Usage

#[main]
fn main() {
    Std.pln("printing a line");
    pln("printing another line"); // no 'Std' needed for this library
}

Std.assert(value: unknown = false) -> void

Throw an error if the given value is not truthy.

assert(true);
assert(false); // errors

Std.assert_eq(first: unknown, second: unknown) -> void

Throw an error if the first value does not equal the second.

assert_eq('a', 'a');
assert_eq(43, 42); // errors

Std.assert_neq(first: unknown, second: unknown) -> void

Throw an error if the first value equals the second.

Std.assert_not(value: unknown = true) -> void

Throw an error if the given value is truthy.

Std.blobify(format: str = "json", context: obj = null) -> blob

Use a loaded format to export a binary blob from the given context (or entire graph/document). The default format is json, and the standard implementation only exports object fields. Export results will vary depending on the format; some support more than others (it is up to the format implementation to decide how it exports data). You can always create your own to use.

Std.callstack() -> list

Return the current call stack as a list of function pointers (last function is 'this').

Std.copy(val: unknown) -> unknown

Deep copy the given value. If this value is an object (or contains one), recursively deep copy the object (all fields, funcs, & data).

Std.dbg(..) -> void

Prints all arguments as debug output to the standard output stream.

Std.dbg_tracestack() -> void

Print a snapshot of the current stack.

Std.drop(..) -> bool | list

Drop fields (by str path), functions (path or fn), objects (path or obj), and data from the graph. Objects will have their #[dropped] functions called when dropped. When dropping multiple values at once, this will return a list of booleans indicating a successful removal or not for each value.

Std.env(var: str) -> str

Get an environment variable by name. Requires the "system" feature flag.

Std.env_vars() -> map

Get a map of the current environment variables (str, str). Requires the "system" feature flag.

Std.err(..) -> void

Prints all arguments to the error output stream.

Std.exit(..) -> void

Immediately terminates this (or another) Stof process. Pass a promise into this function to terminate its process execution.

Std.format(format: str) -> bool

Is the given format loaded/available to use?

Std.format_content_type(format: str) -> str

Returns the available format's content type (HTTP header value), or null if the format is not available. All formats are required to give a content type, even if it doesn't apply to that format.

Std.formats() -> set

A set of all available formats, available to use with parse, stringify, and blobify.

Std.funcs(attributes: str | list | set = null) -> list

Get a list of all functions in this graph, optionally filtering by attributes (single string, list of strings, set of strings, or tuple of strings).

Std.graph_id() -> str

Return this graph's unique string ID.

Std.lib(lib: str) -> bool

Is the given library loaded/available to use?

Std.libs() -> set

Set of all available libraries. This will most likely include standard libraries like Std, Fn, Set, List, etc.

Std.list(..) -> list

Construct a new list with the given arguments.

Std.map(..) -> map

Construct a new map with the given arguments (tuples of key & value). Helpful as a way to create an empty map.

Std.max(..) -> unknown

Return the maximum value of all given arguments. If an argument is a collection, the max value within the collection will be considered only.

Std.min(..) -> unknown

Return the minimum value of all given arguments. If an argument is a collection, the min value within the collection will be considered only.

Std.nanoid(length: int = 21) -> str

Generate a URL-safe random string ID, using the nanoid algorithm with a specified length (default is 21 characters). The probability of a collision is very low and inversely proportional to ID length.

Std.parse(source: str | blob, context: str | obj = self, format: str = "stof") -> bool

Parse data into this document/graph at the given location (default context is the calling object), using the given format (default is Stof). Formats are extensible and replaceable in Stof, so use whichever formats you have loaded (json, stof, images, pdfs, docx, etc.).

Std.peek(..) -> void

Trace this location within your code execution. Will print out your arguments, plus process debug information and the next instructions on the instruction stack. If the last argument given is an integer value, that number of (future) instructions will be shown (very helpful for deeper debugging).

Std.pln(..) -> void

Prints all arguments to the standard output stream.

Std.prompt(text: str = '', tag?: str) -> prompt

A helper function to create a prompt.

Std.remove_env(var: str) -> void

Remove an environment variable by name. Requires the "system" feature flag.

Std.set(..) -> set

Construct a new set with the given arguments.

Std.set_env(var: str, value: str) -> void

Set an environment variable by name with a value. Requires the "system" feature flag.

Std.shallow_drop(..) -> bool | list

Operates the same way Std.drop(..) does, however, if dropping a field and the field points to an object or data, only remove the field and not the associated object/data. This is used instead of drop in instances where multiple fields might point to the same object and you'd like to remove the field without removing the object.

Std.sleep(time: ms) -> void

Instruct this process to sleep for an amount of time, while others continue executing. Use time units for specificity, but don't expect this to be very accurate (guaranteed it will sleep for at least this long, but maybe longer). The default unit is milliseconds.

Std.str(..) -> str

Prints all arguments to a string, just like it would be to an output stream.

Std.stringify(format: str = "json", context: obj = null) -> str

Use a loaded format to export a string from the given context (or entire graph/document). The default format is json, and the standard implementation only exports object fields. Export results will vary depending on the format, some support more than others (it is up to the format implementation to decide how it exports data). You can always create your own to use.

Std.swap(first: unknown, second: unknown) -> void

Swap the memory addresses of any two values.

Std.throw(value: unknown = "Error") -> void

Throw an error with an optional value. Optionally catch this value within a try-catch block. Otherwise, this process will immediately halt execution with the given error.

Std.trace(..) -> void

Trace this location within your code execution. Will print out your arguments, plus process debug information and the current instruction stack. If the last argument given is an integer value, the number of executed instruction stack instructions will be shown (very helpful for deeper debugging).

Std.xml(text: str, tag: str) -> str

A helper function to create an XML-tagged string.

Last updated