Methods & Static Namespaces
Since beta.13, most operations on values use method syntax: value.method(args). Methods are resolved at runtime based on the value’s type tag. Each type has its own set of methods.
Achronyme also provides static namespaces using Type::MEMBER syntax for type-level constants and constructors.
Method Syntax
Section titled “Method Syntax”// Method calllet items = [3, 1, 2]let sorted = items.sort(fn(a, b) { a - b })
// Chaininglet result = [1, 2, 3, 4, 5] .filter(fn(n) { n > 2 }) .map(fn(n) { n * 10 })// result: [30, 40, 50]6 methods on integer values.
.abs()
Section titled “.abs()”Returns the absolute value.
let x = -42assert(x.abs() == 42).min(other)
Section titled “.min(other)”Returns the smaller of two integers.
assert(3.min(7) == 3).max(other)
Section titled “.max(other)”Returns the larger of two integers.
assert(3.max(7) == 7).pow(exp)
Section titled “.pow(exp)”Integer exponentiation. Exponent must be non-negative.
assert(2.pow(10) == 1024).to_field()
Section titled “.to_field()”Converts the integer to a BN254 field element.
let f = 42.to_field()assert(typeof(f) == "Field").to_string()
Section titled “.to_string()”Returns the string representation.
assert(42.to_string() == "42")Static: Int::MAX, Int::MIN
Section titled “Static: Int::MAX, Int::MIN”60-bit signed integer bounds.
print(Int::MAX) // 576460752303423487 (2^59 - 1)print(Int::MIN) // -576460752303423488 (-2^59)String
Section titled “String”14 methods on string values.
.len()
Section titled “.len()”Returns the character count.
assert("hello".len() == 5).starts_with(prefix)
Section titled “.starts_with(prefix)”Returns true if the string starts with the given prefix.
assert("hello".starts_with("hel")).ends_with(suffix)
Section titled “.ends_with(suffix)”Returns true if the string ends with the given suffix.
assert("hello".ends_with("llo")).contains(substr)
Section titled “.contains(substr)”Returns true if the string contains the substring.
assert("hello world".contains("world")).split(delimiter)
Section titled “.split(delimiter)”Splits the string by the given delimiter. Returns a list of strings.
assert("a,b,c".split(",") == ["a", "b", "c"]).trim()
Section titled “.trim()”Removes leading and trailing whitespace.
assert(" hello ".trim() == "hello").replace(search, replacement)
Section titled “.replace(search, replacement)”Replaces all occurrences of search with replacement.
assert("aabbcc".replace("bb", "XX") == "aaXXcc").to_upper()
Section titled “.to_upper()”Converts all characters to uppercase.
assert("hello".to_upper() == "HELLO").to_lower()
Section titled “.to_lower()”Converts all characters to lowercase.
assert("HELLO".to_lower() == "hello").chars()
Section titled “.chars()”Splits into a list of individual characters.
assert("abc".chars() == ["a", "b", "c"]).index_of(substr)
Section titled “.index_of(substr)”Returns the character index of the first occurrence. Returns -1 if not found.
assert("hello world".index_of("world") == 6)assert("hello".index_of("xyz") == -1).substring(start, end)
Section titled “.substring(start, end)”Extracts a substring from position start to end (exclusive). Indices are clamped to string bounds.
assert("hello".substring(1, 4) == "ell").repeat(n)
Section titled “.repeat(n)”Repeats the string n times. Maximum result size is 10 MB.
assert("ab".repeat(3) == "ababab").to_string()
Section titled “.to_string()”Returns the string itself (identity).
assert("hi".to_string() == "hi")13 methods on list values.
.len()
Section titled “.len()”Returns the element count.
assert([1, 2, 3].len() == 3).push(item)
Section titled “.push(item)”Appends an item to the end. Mutates the list in-place. Returns nil.
mut items = [1, 2]items.push(3)assert(items.len() == 3).pop()
Section titled “.pop()”Removes and returns the last element. Returns nil if empty.
mut items = [1, 2, 3]assert(items.pop() == 3)assert(items.len() == 2).map(fn)
Section titled “.map(fn)”Returns a new list with fn(elem) applied to each element.
let doubled = [1, 2, 3].map(fn(n) { n * 2 })assert(doubled == [2, 4, 6]).filter(fn)
Section titled “.filter(fn)”Returns a new list containing only elements where fn(elem) is truthy.
let evens = [1, 2, 3, 4].filter(fn(n) { n % 2 == 0 })assert(evens == [2, 4]).reduce(initial, fn)
Section titled “.reduce(initial, fn)”Folds the list into a single value: acc = fn(acc, elem) for each element.
let sum = [1, 2, 3].reduce(0, fn(acc, n) { acc + n })assert(sum == 6).for_each(fn)
Section titled “.for_each(fn)”Calls fn(elem) for each element (for side effects). Returns nil.
[1, 2, 3].for_each(fn(n) { print(n) }).find(fn)
Section titled “.find(fn)”Returns the first element where fn(elem) is truthy, or nil if none match.
let found = [1, 2, 3, 4].find(fn(n) { n > 2 })assert(found == 3).any(fn)
Section titled “.any(fn)”Returns true if fn(elem) is truthy for any element. Short-circuits.
assert([1, 2, 3].any(fn(n) { n == 2 })).all(fn)
Section titled “.all(fn)”Returns true if fn(elem) is truthy for all elements. Short-circuits.
assert([1, 2, 3].all(fn(n) { n > 0 })).sort(fn)
Section titled “.sort(fn)”Returns a new sorted list. The comparator fn(a, b) must return negative if a < b, zero if equal, positive if a > b.
let sorted = [3, 1, 2].sort(fn(a, b) { a - b })assert(sorted == [1, 2, 3]).flat_map(fn)
Section titled “.flat_map(fn)”Like .map(), but flattens one level of nested lists.
let result = [1, 2, 3].flat_map(fn(n) { [n, n * 10] })assert(result == [1, 10, 2, 20, 3, 30]).zip(other)
Section titled “.zip(other)”Returns a list of [a, b] pairs, truncated to the shorter list.
let pairs = [1, 2, 3].zip([10, 20, 30])assert(pairs == [[1, 10], [2, 20], [3, 30]])8 methods on map values.
.len()
Section titled “.len()”Returns the number of key-value pairs.
let m = {name: "Alice", age: 30}assert(m.len() == 2).keys()
Section titled “.keys()”Returns a list of all keys.
let m = {name: "Alice", age: 30}let k = m.keys()assert(k.len() == 2).values()
Section titled “.values()”Returns a list of all values.
let m = {a: 1, b: 2, c: 3}let v = m.values()assert(v.len() == 3).entries()
Section titled “.entries()”Returns a list of [key, value] pairs.
let m = {x: 10, y: 20}let e = m.entries()// e contains [["x", 10], ["y", 20]]assert(e.len() == 2).contains_key(key)
Section titled “.contains_key(key)”Returns true if the map contains the given key.
let m = {name: "Alice"}assert(m.contains_key("name"))assert(!m.contains_key("age")).get(key, default)
Section titled “.get(key, default)”Returns the value for key, or default if the key doesn’t exist.
let m = {name: "Alice"}assert(m.get("name", "?") == "Alice")assert(m.get("age", 0) == 0).set(key, value)
Section titled “.set(key, value)”Sets or updates a key-value pair. Mutates the map in-place. Returns nil.
mut m = {}m.set("name", "Alice")m.set("age", 30)assert(m.len() == 2)assert(m.name == "Alice").remove(key)
Section titled “.remove(key)”Removes a key and returns its value. Returns nil if the key doesn’t exist.
mut m = {name: "Alice", age: 30}let removed = m.remove("age")assert(removed == 30)assert(m.len() == 1)2 methods on BN254 field elements.
.to_int()
Section titled “.to_int()”Converts the field element to an integer. The value must fit in 64 bits.
let f = 0p42assert(f.to_int() == 42).to_string()
Section titled “.to_string()”Returns the decimal string representation.
let f = 0p42assert(f.to_string() == "42")Static: Field::ZERO, Field::ONE, Field::ORDER
Section titled “Static: Field::ZERO, Field::ONE, Field::ORDER”Field constants for BN254 Fr.
let z = Field::ZERO // 0p0let o = Field::ONE // 0p1let q = Field::ORDER // "21888242871839275222246405745257275088548364400416034343698204186575808495617"BigInt
Section titled “BigInt”7 methods on BigInt256 and BigInt512 values.
.to_bits()
Section titled “.to_bits()”Converts to a list of 0/1 integers in LSB-first order. Length is 256 or 512 depending on width.
let bits = bigint256(42).to_bits()// bits[0] = 0, bits[1] = 1, bits[2] = 0, bits[3] = 1, bits[4] = 0, bits[5] = 1.bit_and(other)
Section titled “.bit_and(other)”Bitwise AND. Both operands must have the same width.
let result = bigint256("0xFF").bit_and(bigint256("0x0F"))assert(result == bigint256("0x0F")).bit_or(other)
Section titled “.bit_or(other)”Bitwise OR. Both operands must have the same width.
let result = bigint256("0xF0").bit_or(bigint256("0x0F"))assert(result == bigint256("0xFF")).bit_xor(other)
Section titled “.bit_xor(other)”Bitwise XOR. Both operands must have the same width.
let result = bigint256("0xFF").bit_xor(bigint256("0x0F"))assert(result == bigint256("0xF0")).bit_not()
Section titled “.bit_not()”Bitwise NOT (flips all bits).
let x = bigint256(0).bit_not() // all bits set.bit_shl(n)
Section titled “.bit_shl(n)”Shifts left by n bits. Errors if any set bits are shifted out (overflow).
assert(bigint256(1).bit_shl(8) == bigint256(256)).bit_shr(n)
Section titled “.bit_shr(n)”Shifts right by n bits. Bits shifted out are discarded.
assert(bigint256(256).bit_shr(8) == bigint256(1))Static: BigInt::from_bits
Section titled “Static: BigInt::from_bits”Alias for the global from_bits function.
let val = BigInt::from_bits(bigint256(42).to_bits(), 256)assert(val == bigint256(42))Summary Table
Section titled “Summary Table”Methods by Type
Section titled “Methods by Type”| Type | Method | Args | Returns |
|---|---|---|---|
| Int | .abs() | 0 | Int |
.min(other) | 1 | Int | |
.max(other) | 1 | Int | |
.pow(exp) | 1 | Int | |
.to_field() | 0 | Field | |
.to_string() | 0 | String | |
| String | .len() | 0 | Int |
.starts_with(prefix) | 1 | Bool | |
.ends_with(suffix) | 1 | Bool | |
.contains(substr) | 1 | Bool | |
.split(delim) | 1 | List | |
.trim() | 0 | String | |
.replace(search, repl) | 2 | String | |
.to_upper() | 0 | String | |
.to_lower() | 0 | String | |
.chars() | 0 | List | |
.index_of(substr) | 1 | Int | |
.substring(start, end) | 2 | String | |
.repeat(n) | 1 | String | |
.to_string() | 0 | String | |
| List | .len() | 0 | Int |
.push(item) | 1 | nil | |
.pop() | 0 | value | |
.map(fn) | 1 | List | |
.filter(fn) | 1 | List | |
.reduce(init, fn) | 2 | value | |
.for_each(fn) | 1 | nil | |
.find(fn) | 1 | value | |
.any(fn) | 1 | Bool | |
.all(fn) | 1 | Bool | |
.sort(fn) | 1 | List | |
.flat_map(fn) | 1 | List | |
.zip(other) | 1 | List | |
| Map | .len() | 0 | Int |
.keys() | 0 | List | |
.values() | 0 | List | |
.entries() | 0 | List | |
.contains_key(key) | 1 | Bool | |
.get(key, default) | 2 | value | |
.set(key, value) | 2 | nil | |
.remove(key) | 1 | value | |
| Field | .to_int() | 0 | Int |
.to_string() | 0 | String | |
| BigInt | .to_bits() | 0 | List |
.bit_and(other) | 1 | BigInt | |
.bit_or(other) | 1 | BigInt | |
.bit_xor(other) | 1 | BigInt | |
.bit_not() | 0 | BigInt | |
.bit_shl(n) | 1 | BigInt | |
.bit_shr(n) | 1 | BigInt |
Static Namespaces
Section titled “Static Namespaces”| Namespace | Member | Value |
|---|---|---|
Int::MAX | 576460752303423487 (2^59 - 1) | |
Int::MIN | -576460752303423488 (-2^59) | |
Field::ZERO | Field element 0 | |
Field::ONE | Field element 1 | |
Field::ORDER | BN254 Fr modulus (string) | |
BigInt::from_bits | from_bits(bits, width) constructor |