Utility Functions
General-purpose built-in functions for type inspection, string formatting, error handling, and control flow.
type
Return the type name of a value as a string.
type(42) // "int"
type(3.14) // "float"
type("hello") // "string"
type(true) // "bool"
type([1, 2]) // "list"
type({a: 1}) // "map"
type(nil) // "nil"
type(x => x) // "function"sprintf
Format a string using Go fmt.Sprintf syntax. Supports format verbs like %s,
%d, %f, %v, and others.
sprintf("Hello, %s!", "world") // "Hello, world!"
sprintf("%d items at $%.2f", 3, 9.99) // "3 items at $9.99"
sprintf("%v", [1, 2, 3]) // "[1, 2, 3]"
sprintf("%04d", 42) // "0042"error
Create an error value. This function creates the error but does not throw it --
the error is returned as a regular value. Supports format arguments like
sprintf.
let err = error("file not found: %s", "config.json")
type(err) // "error"
err.message() // "file not found: config.json"To raise an error so that it halts execution, use assert or the throw
statement instead.
assert
Raise an error if the given condition is false. An optional message can be provided.
assert(len(items) > 0)
assert(len(items) > 0, "items must not be empty")
let x = 42
assert(x > 0, sprintf("expected positive number, got %d", x))When the condition is true, assert returns nil and execution continues
normally.
getattr
Safely access an attribute on an object. Returns the attribute value if it exists, or an optional default value if it does not. Without a default, raises an error for missing attributes.
let config = {host: "localhost", port: 8080}
getattr(config, "host") // "localhost"
getattr(config, "timeout", 30) // 30
getattr(config, "timeout") // error: attribute "timeout" not foundcall
Call a function dynamically by passing it as the first argument along with its arguments.
let add = (a, b) => a + b
call(add, 3, 4) // 7
let greet = name => sprintf("Hello, %s!", name)
call(greet, "Risor") // "Hello, Risor!"This is useful when you have a function reference and want to invoke it dynamically, for example from a map of named handlers.
any
Return true if any element in the given list or iterator is truthy.
any([false, false, true]) // true
any([0, 0, 0]) // false
any([0, 1, 0]) // true
any([]) // falseall
Return true if all elements in the given list or iterator are truthy.
all([true, true, true]) // true
all([true, false, true]) // false
all([1, 2, 3]) // true
all([1, 0, 3]) // false
all([]) // truecoalesce
Return the first non-nil argument. This is useful for providing fallback values.
coalesce(nil, nil, "default") // "default"
coalesce(nil, 42, "ignored") // 42
coalesce("first", nil, "second") // "first"
// Common pattern: fall back through multiple sources
let value = coalesce(
config.get("timeout"),
env.get("TIMEOUT"),
30
)