Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Built-in methods

Methods are called with a dot and they chain. Every signature below was checked against the implementation rather than transcribed.

String

MethodResultNotes
upper() / lower()String"auth".upper() → "AUTH"
len()Intcounts characters: "héllo".len() → 5
trim()Stringstrips surrounding whitespace
split(sep)List"a,b".split(",") → ["a", "b"]
replace(from, to)Stringevery occurrence
starts_with(prefix) / ends_with(suffix)Bool
contains(substr)Bool"hello".contains("ell") → true
to_int() / to_float()Int / Floata malformed number is an error, not null
slice(start, end)Stringsubstring by character index, end exclusive, indices clamped
to_str()Stringidentity, for use in chains
parse_json() / parse_yaml() / parse_toml()Objectintegers become Int; a failure is E0314
↳ fidelitykey order is the document’s; YAML << is merged; a TOML datetime becomes a String
parse_duration()Int (seconds)"1h30m" → 5400; units d/h/m/s; E0319
parse_datetime()Int (epoch UTC)RFC 3339 or YYYY-MM-DD; offsets ±HH:MM; E0320
sha256()Stringlower-case hex digest
base64() / base64_decode()Stringstandard alphabet with padding; invalid input is E0321

Int

MethodResultNotes
abs()Int
to_float()Floatwidening; there is no Float.to_int(), which would have to round
to_str()String
format_duration()String5400 → "1h30m"; 0 → "0s"
format_datetime()StringRFC 3339 UTC: 946684800 → "2000-01-01T00:00:00Z"

Float

MethodResult
abs()Float
round(digits)Float
to_str()String

Bool

MethodResult
to_str()String

List

MethodResultNotes
len()Int
first() / last()elementan empty list is E0317
get(i) / get(i, default)elementa miss gives null, or the default if you supply one
contains(item)Boolstructural equality
compact()Listdrops null
uniq()Listdeduplicates, keeping first occurrences in order
map (x, i) -> ... endListthe callback receives the element and its index
filter (x, i) -> Bool endLista non-Bool from the callback is E0306
join(sep)Stringscalars only; containers are E0307
sort()List
reverse()List
sum()Int
min() / max()element
flatten()Listone level: [[1], [2, 3]] → [1, 2, 3]
to_object()Objectbuilds an object from { key, value } pairs; a repeated key is E0323
reduce(init) (acc, e) -> … endAnyleft fold; the initial value is required
any (e, i) -> Bool endBoolsome element matches; false for an empty list
all (e, i) -> Bool endBoolevery element matches; true for an empty list
find(default) (e, i) -> Bool endAnyfirst match, or the default
index_of(value, default)Intindex of the first equal element, or the default; the default must be an Int
group_by (e, i) -> key endObjectelements by a String key, keys in the order they first appear (D39)
sort_by (e, i) -> key endListascending and stable; keys follow sort() (D39)
flat_map (e, i) -> [...] endListmap, then the lists joined one level; a non-List from the lambda is E0306 (D39)
zip(other)List{ left, right } pairs; lists of different lengths are E0327 (D39)
slice(start, end)Listend exclusive: [1, 2, 3].slice(1, 3) → [2, 3]
to_json() / to_yaml() / to_toml()Stringthe compact form, for embedding; to_toml on a list is E0603
to_json_pretty() / to_yaml_pretty() / to_toml_pretty()Stringthe form a person reads, for a file that gets diffed

Object

MethodResultNotes
len()Int
keys() / values()Listdeclaration order
contains(key)Bool
get(key, default)valuea miss gives the default
entries()List{ key, value } objects in declaration order; the inverse of to_object()
merge(other)Objectthe right side’s keys win
to_json() / to_yaml() / to_toml()Stringthe compact form, for embedding
to_json_pretty() / to_yaml_pretty() / to_toml_pretty()Stringthe form a person reads, for a file that gets diffed

Operators that build values

Most operators are numeric. Two are not, and they are the only ones whose result can be larger than what went in.

ExpressionResultNotes
"a" + "b"Stringjoins two strings; interpolation "#{a}#{b}" does the same
[1] + [2, 3]Listjoins two lists; the operands are unchanged

+ never converts. "port " + 8080 is E0306, and the diagnostic names the side to call .to_str() on. Joining is bounded at 1,000,000 list elements and 16 MiB of string, reported as E0322.

Global functions

FunctionRight requiredBehaviour
range(n)—[0, 1, … n-1]
env(name, default)--allow-envan environment variable; absent → the default
read_file(path)--allow-readthe file’s contents as a string; a relative path is read from the manifest’s folder (D37)
fail(message)—stops with E0531 and your message

A method call takes precedence over an object field of the same name that happens to hold a function. A package’s exported functions are called with the same syntax: pkg.fn(...).