Complete API reference for OtterLang standard library functions and types.
Note
Only the prelude primitives (print, panic, len, strings/lists/maps, enums/Option/Result, arithmetic) are injected automatically. Every other module documented here must be imported explicitly with use module_name (e.g. use http, use json as js) before its dotted members can be referenced.
- Built-in Functions
- Module:
io- Input/Output Operations - Module:
math- Mathematical Functions - Module:
time- Time and Date Operations - Module:
json- JSON Processing - Module:
runtime- Runtime Utilities - Module:
arena- Memory Arenas - Module:
task- Concurrent Task Execution - Type Definitions
Core functions available in all OtterLang programs.
Prints a message to standard output.
Parameters:
message: The string to print
Example:
print("Hello, World!")
Prints a message to standard output followed by a newline.
Parameters:
message: The string to print
Example:
println("Hello, World!")
Prints a newline to standard output.
Example:
println() # Just prints a newline
Prints a message to standard error followed by a newline (useful for diagnostics).
Example:
eprintln("Something went wrong")
Converts any value to its string representation.
Parameters:
value: The value to convert
Returns: String representation of the value
Example:
answer = str(42)
println(f"Value: {answer}")
Returns the length of an array or string.
Parameters:
collection: An array or string
Returns: The length as an integer
Example:
array_len = len([1, 2, 3]) # Returns 3
str_len = len("hello") # Returns 5
Returns the capacity of an array.
Parameters:
array: An array
Returns: The capacity as an integer
Wrappers around the runtime I/O primitives (src/runtime/stdlib/io.rs). None of these functions are in the prelude, so use io is required.
Loads the entire file at path into a string. Raises a runtime error if the file cannot be read.
Writes data into path, overwriting any existing file. Returns true on success.
Copies src to dst. Returns true when the operation succeeds.
Returns all lines from the file as a list of strings (newline terminators removed).
Creates an in-memory buffer seeded with data. Buffers expose helper functions:
buffer_read(buf, n)– returns up tonbytes from the buffer.buffer_write(buf, bytes)– appends bytes and returnstrueon success.buffer_clear(buf)– empties the buffer.buffer_data(buf)– returns the entire buffer contents as a string.
Functions exposed once use math is invoked. All functions take/return float unless noted.
abs(x)– absolute value.sqrt(x)– square root; panics on negative inputs.pow(x, y)–xraised toy.exp(x)/log(x)– natural exponential/logarithm.sin(x),cos(x),tan(x)– trigonometric helpers (radians).atan2(y, x)– four-quadrant arctangent.floor(x),ceil(x),round(x)– rounding helpers.clamp(x, min, max)– clamp value into[min, max].min(a, b)/max(a, b)– pairwise extrema.hypot(x, y)– √(x² + y²).lerp(a, b, t)– linear interpolation.randf()– pseudo‑random float in [0, 1).randi(max: int) -> int– pseudo‑random integer in[0, max).mean(list<float>, len)/std(...)/sum(...)– statistics helpers that operate over the firstlenelements of a float list.
Returns the current time in milliseconds since Unix epoch.
Returns: Milliseconds since epoch as an integer
Example:
timestamp = now_ms()
Sleeps for the specified number of milliseconds.
Parameters:
ms: Milliseconds to sleep
Example:
sleep_ms(1000) # Sleep for 1 second
Parses a JSON string into a dictionary or array.
Parameters:
json_str: A valid JSON string
Returns: Parsed value or nil on error
Example:
data = json.parse('{"name": "Otter", "age": 42}')
if data != nil:
name = data["name"]
Converts a dictionary or array to a JSON string.
Parameters:
value: A dictionary or array
Returns: JSON string representation
Example:
json_str = json.stringify({"key": "value"})
Note
stringify() is specific to JSON serialization. For general-purpose conversions use the built-in str() helper described above (the old Pythonic alias relationship has been flipped: stringify() now simply calls str()).
Manually triggers garbage collection.
Returns: Number of bytes freed
Example:
freed_bytes = runtime.collect_garbage()
println(f"Freed {freed_bytes} bytes")
Sets the garbage collection strategy.
Parameters:
strategy: One of "rc", "marksweep", "hybrid", "none"
Example:
runtime.set_gc_strategy("marksweep")
Allocates size bytes on the GC-managed heap and returns a pointer.
Parameters:
size: Number of bytes to allocate
Returns: Pointer as an integer
Example:
ptr = gc.alloc(128)
Registers the given pointer as a GC root.
Parameters:
ptr: Pointer returned bygc.alloc
Example:
gc.add_root(ptr)
Removes a previously registered root pointer.
Parameters:
ptr: Pointer previously added withgc.add_root
Example:
gc.remove_root(ptr)
Starts memory profiling.
Example:
runtime.memory_profiler_start()
Stops memory profiling.
Example:
runtime.memory_profiler_stop()
Returns memory profiling statistics as JSON.
Returns: JSON string with profiling statistics
Example:
stats = runtime.memory_profiler_stats()
Detects and returns memory leaks as JSON.
Returns: JSON string with leak information
Example:
leaks = runtime.memory_profiler_leaks()
Temporarily pauses automatic garbage collection.
Returns: Previous GC state
Example:
was_enabled = gc.disable()
# Perform operations that need GC disabled
if was_enabled:
gc.enable()
Re-enables automatic garbage collection.
Returns: Previous GC state
Example:
gc.enable()
Returns true if garbage collection is currently active.
Returns: Boolean indicating GC state
Example:
if not gc.is_enabled():
gc.enable()
Lightweight bump-allocated arenas for deterministic lifetimes. Arenas do not participate in GC; all allocations live until you reset or destroy the arena.
Creates a new arena with the requested capacity (default 64KB) and returns a handle.
Parameters:
capacity: Initial capacity in bytes (default: 65536)
Returns: Arena handle as integer
Example:
arena_handle = arena.create(131072) # 128KB arena
Allocates raw bytes from the arena. Returns a pointer (as an integer) or 0 if there is not enough space.
Parameters:
handle: Arena handle fromarena.createsize: Number of bytes to allocatealign: Alignment requirement (default: 8)
Returns: Pointer as integer, or 0 on failure
Example:
ptr = arena.alloc(arena_handle, 128)
Clears all allocations inside the arena so it can be reused.
Parameters:
handle: Arena handle
Returns: true on success
Example:
arena.reset(arena_handle)
Destroys the arena and frees its backing memory.
Parameters:
handle: Arena handle
Returns: true on success
Example:
arena.destroy(arena_handle)
Spawns a concurrent task.
Parameters:
block: A function block to execute concurrently
Returns: A Task handle
Example:
task = spawn:
return compute_result()
Waits for a task to complete and returns its result.
Parameters:
task: A Task handle
Returns: The task's result
Example:
task = spawn:
return 42
result = await task
Represents a concurrent task that returns type T.
int: 64-bit signed integerfloat: 64-bit floating pointbool: Boolean (trueorfalse)string: UTF-8 stringunit: Unit type (void)array<T>: Dynamic array of type Tdict<K, V>: Dictionary mapping K to V
See Language Specification for more details.