Built-in functions

Built-in functions

print(value)

Writes one value, followed by a new line.

print("text")
print(42)
print(2.5)
print(true)
print([1, 2, 3])

Output:

text
42
2.5
true
[1, 2, 3]
TypePrinted as
strthe text itself, without quotes
int, bytedecimal number
floatthe shortest exact form, always with a decimal point or exponent: 1.0, 0.1, 1e+16, inf, nan (see float)
booltrue or false
list[a, b, c]; strings inside in quotes (see Lists)
map{key: value, ...} in insertion order; strings inside in quotes (see Maps)
set{a, b, ...} in insertion order; strings inside in quotes (see Sets)
errorerror CODE: message, or nil

print takes exactly one value. To print several things, call it several times.

stdout.write and stderr.write

stdout.write(text) writes a str without a new line after it, so a line can be built in pieces: a progress bar, an answer streamed as it comes. stderr.write(text) writes to the error output instead, where messages go when the program’s output is for something else: a program in a pipe, an MCP server on stdio.

stdout.write("Loading")
stdout.write("...")
stdout.write(" done\n")
n: int = 3
stdout.write("${n} files\n")

Output:

Loading... done
3 files
  • stderr.write("warning: no config file\n") writes to the error output: shown in a terminal, but not sent on by luma run app.luma > out.txt or a pipe.
  • They take a str; put another value in one: stdout.write("${n}"). Neither adds a new line: write \n where you want one.
  • What stdout.write writes shows at once, also in luma run.
  • print writes to the same output, so the two can be mixed.

prompt(text)

Shows text (without a new line after it), reads one line typed by the user, and gives it back. The type of the variable it goes into decides how the line is read: age: int = prompt("Age: ") reads an int. prompt() without a text just reads a line. See Reading input.

age: int = prompt("Age: ")
print(age * 2)

Input:

21

Output:

Age: 42

flag(name, default)

Gives the value of the command-line flag --name, or default when the program was started without it. The default decides the type, and both must be written directly. See Command-line flags.

port: int = flag("port", 8080)
print(port)

Run with:

luma run app.luma --port=9000

Output:

9000

file(path)

Makes a file from its path. Nothing is opened or created until one of its methods is used: read, write, add, exists, delete, size. See Files. file.this() is the .luma file it is written in: see Where this file is.

f: file = file("hello.txt")
f.write("Hi!")
print(f.read())

Output:

Hi!

folder(path)

Makes a folder from its path, as file makes a file: make, exists, delete, files, folders, file(name), folder(name). See Folders.

shell(command)

Runs a command and gives what it wrote, as a str (without the line breaks at its end); shell([program, arguments...]) runs a program without a shell in between. A command that fails raises error 16. See Running commands.

print(shell("echo hi"))

Output:

hi

free(variable)

Ends a variable before its block ends: its value is freed (if nothing else holds it), and the name is gone until it is declared again. It must be in the block where the variable was declared. See Memory.

big: [int] = [1, 2, 3]
total: int = len(big)
free(big)
print(total)

Output:

3

convert(value)

Converts a value to the type the surrounding code expects: n: int = convert("42"). A value that doesn’t fit raises error 11. See Converting between types.

n: int = convert("42")
s: str = convert(n * 2)
print(s)

Output:

84

schema(ObjType)

Gives the JSON Schema of an obj type, as a json: what an AI model is told a tool’s input or its answer must look like. Field comments become descriptions. See A schema from a type.

obj Point {
    x: float
    y: float = 0.0     // 0 if not given
}
print(schema(Point))

Output:

{"type":"object","properties":{"x":{"type":"number"},"y":{"type":"number","description":"0 if not given"}},"required":["x"],"additionalProperties":false}

len(list) / len(map) / len(set) / len(text)

Gives the number of elements in a list or set, the number of keys in a map, or the number of characters in a string, as an int.

nums: [int] = [10, 20, 30]
ages: {str: int} = {"Alice": 30}
print(len(nums))
print(len(ages))
print(len("tööd"))

Output:

3
1
4

Strings also have methods, like text.upper() and text.split(","): see Working with text. Floats have round, decimals, floor, ceil and trunc: see Rounding.

remove(list, index) / remove(map, key) / remove(set, value)

For a list, removes the element at index. The elements after it move one place forward.

For a map, removes the key and its value.

For a set, removes the value.

If the index is outside the list, or the key or value is not in the map or set, the program stops with a runtime error.

nums: [int] = [10, 20, 30]
remove(nums, 1)
print(nums)
ages: {str: int} = {"Alice": 30, "Bob": 25}
remove(ages, "Alice")
print(ages)

Output:

[10, 30]
{"Bob": 25}

remove doesn’t give back a value.

has(map, key) / has(set, value)

Gives true if the key is in the map, or the value is in the set, otherwise false. Use it before reading a key that might be missing:

ages: {str: int} = {"Alice": 30}
print(has(ages, "Alice"))
print(has(ages, "Carol"))

Output:

true
false

union, intersect and diff

They are methods of sets and lists: a.union(b), a.intersect(b), a.diff(b). See Sets and Lists.

error(code, message)

Makes an error value with an int code (100 or more) and a str message, usually to raise it with throw. See Error handling.

err: error = nil
try err {
    throw error(100, "Disk is full")
}
print(err)

Output:

error 100: Disk is full