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

8. Built-in functions and methods

Korrin has no import for its core vocabulary: a small set of functions is bound in every program's global scope, and the built-in types str, list, and map carry methods reached with ..

The division is deliberate (ADR 0016):

  • A function constructs or converts a value, or does I/O — print, str, range. It is not an operation on a particular value.
  • A method operates on its receiver — xs.push(v), text.trim(), m.keys().

Argument count is checked before the function or method runs (E0303); argument types are checked by each (E0304 unless noted). For a method, the receiver is not counted as an argument.

Every name below must match the interpreter exactly; crates/korrin/tests/builtins.rs fails the build otherwise (ADR 0002).

One further name, Error, is bound in the global scope: it is the built-in exception type, documented with the error model in §9.6.

8.1 Functions

print(*values)

Writes each value's display form (see §3.6), separated by single spaces, followed by a newline. print() writes just a newline.

print("x", "=", 3)
# => x = 3

input(prompt?)

Writes prompt (if given, with no trailing newline) to standard output, then reads and returns one line from standard input as a str with its trailing newline removed. Returns nil at end of input.

type(value)

The name of value's type as a str: "nil", "bool", "int", "float", "str", "list", "map", "function", "class", or — for an instance — the class's name.

print(type(1))
print(type(1.0))
print(type([]))
# => int
# => float
# => list

str(value)

value's display form as a str (what print would write).

int(value)

Converts to int: an int unchanged; a bool to 0 or 1; a float truncated toward zero (E0005 if outside the 64-bit range); a str parsed as a decimal integer (surrounding whitespace ignored; E0304 if it is not a valid integer). Other types are E0304.

print(int("42") + 1)
print(int(3.9))
# => 43
# => 3

float(value)

Converts to float: a float unchanged; an int or bool widened; a str parsed (E0304 on failure). Other types are E0304.

bool(value)

value's truthiness as a boolfalse only for nil and false (ADR 0009).

range(stop) / range(start, stop) / range(start, stop, step)

Returns a list of ints from start (default 0) up to but not including stop, in increments of step (default 1). step may be negative; a step of 0 is E0304. All arguments must be ints.

print(range(4))
print(range(2, 5))
print(range(6, 0, -2))
# => [0, 1, 2, 3]
# => [2, 3, 4]
# => [6, 4, 2]

8.2 Methods on str

Strings are immutable, so every str method returns a new value; the receiver is unchanged. Index arguments and results count characters, like [] indexing (§4.4).

MethodResult
str.len()number of characters, as an int
str.upper() / str.lower()case-folded copy
str.trim()copy with leading and trailing whitespace removed
str.split(sep)list of the pieces between each sep; sep must be a non-empty str
str.replace(old, new)copy with every old replaced by new; old must be non-empty
str.find(sub)character index of the first sub, or -1
str.contains(sub)whether sub occurs, as a bool
str.starts_with(prefix) / str.ends_with(suffix)bool
str.slice(start, end)characters start up to (not including) end; bounds are clamped to 0..len
str.repeat(n)n copies joined; n must be ≥ 0
print("  Grace Hopper  ".trim().upper())
print("a,b,c".split(","))
print("na".repeat(4) + " batman")
print("hello".slice(1, 4))
# => GRACE HOPPER
# => ["a", "b", "c"]
# => nananana batman
# => ell

8.3 Methods on list

Lists are mutable and shared. push, insert, remove_at, reverse, and sort change the list in place; push, insert, reverse, and sort return nil, while pop and remove_at return the element removed.

MethodResult
list.len()number of elements, as an int
list.push(value)appends value; returns nil
list.pop()removes and returns the last element; E0307 if empty
list.insert(index, value)inserts before index (0..=len); E0307 otherwise
list.remove_at(index)removes and returns the element at index (0..len); E0307 otherwise
list.index_of(value)index of the first element equal to value, or -1
list.contains(value)whether any element equals value, as a bool
list.slice(start, end)a new list of elements start up to end; bounds clamped to 0..len
list.reverse()reverses in place; returns nil
list.sort()sorts in place, ascending; returns nil. Every element must be a number, or every element a str (E0304 otherwise)
list.join(sep)a str of the elements separated by sep; every element must be a str
xs = [3, 1, 2]
xs.push(0)
xs.sort()
print(xs)
print(["ready", "set", "go"].join(" "))
# => [0, 1, 2, 3]
# => ready set go

8.4 Methods on map

MethodResult
map.len()number of entries, as an int
map.keys() / map.values()a list of the keys / values, in insertion order
map.has(key)whether key is present, as a bool
map.get(key, default?)the value for key, or default (or nil) if absent; never raises E0310
map.remove(key)removes key, returning its value, or nil if absent
map.pairs()a list of [key, value] pairs, in insertion order
scores = {"ada": 10, "alan": 8}
print(scores.keys())
print(scores.get("linus", 0))
scores.remove("ada")
print(scores.has("ada"))
# => ["ada", "alan"]
# => 0
# => false