Built-in Functions

These functions are built into the language and always available — no imports needed.

Utility Functions

input()

Reads a line of text from stdin.

import @std

std.printf("Enter your name: ")
temp name string = input()
std.println("Hello, " + name)

read_int()

Reads an integer from stdin.

import @std

std.printf("Enter a number: ")
temp num int = read_int()
std.println("You entered: " + string(num))

len()

Returns the length of a string, array, or map.

import @std

temp name string = "Hello"
std.println(len(name))  // 5

temp nums [int] = {1, 2, 3}
std.println(len(nums))  // 3

temp ages map[string:int] = {"Alice": 30, "Bob": 25}
std.println(len(ages))  // 2

range()

Generates a sequence of numbers for for loops.

import @std

// range(start, end) - end is exclusive
for i in range(0, 5) {
    std.println(i)  // 0, 1, 2, 3, 4
}

// range(start, end, step)
for i in range(0, 10, 2) {
    std.println(i)  // 0, 2, 4, 6, 8
}

typeof()

Returns the type of a value as a string.

import @std

temp x int = 42
std.println(typeof(x))  // "int"

temp arr [string] = {"a", "b"}
std.println(typeof(arr))  // "array"

copy()

Creates a deep copy of a value. Use this when you need an independent copy rather than a reference.

const Person struct {
    name string
    age int
}

temp a Person = Person{name: "Alice", age: 30}
temp b Person = copy(a)
b.age = 31
// a.age is still 30 - b is an independent copy

Deep copy behavior:

  • Primitives return themselves
  • Nested structs are recursively copied
  • Arrays are copied with all elements
  • Maps are copied with all key-value pairs

Type Conversion Functions

Convert values between types. These are essential for working with user input, formatting output, and data transformations.

int()

Converts a value to an integer.

temp s string = "42"
temp n int = int(s)  // 42

temp f float = 3.9
temp i int = int(f)  // 3 (truncates)

float()

Converts a value to a float.

temp n int = 42
temp f float = float(n)  // 42.0

temp s string = "3.14"
temp pi float = float(s)  // 3.14

string()

Converts a value to a string.

temp n int = 42
temp s string = string(n)  // "42"

temp b bool = true
temp bs string = string(b)  // "true"

char()

Converts an integer (ASCII/Unicode value) to a character.

temp x int = 65
temp c char = char(x)  // 'A'

temp newline char = char(10)  // newline character

Error Handling

Error Type

The Error type represents an error value that can be returned from functions.

FieldTypeDescription
messagestringThe error message
codestringError code (empty for user errors, E-codes for stdlib errors)

Checking for errors:

temp err = validate("")
if err != nil {
    println("Error: ${err.message}")
}

error()

Creates a user-defined error.

Syntax:

error(message string) -> Error
ParameterTypeDescription
messagestringThe error message

Returns: An Error with .message set to the argument and .code set to empty string.

Single error return:

do validate(name string) -> Error {
    if len(name) == 0 {
        return error("name cannot be empty")
    }
    return nil
}

do main() {
    temp err = validate("")
    if err != nil {
        println(err.message)  // "name cannot be empty"
    }
}

Tuple return (value + error):

do divide(a int, b int) -> (int, Error) {
    if b == 0 {
        return 0, error("division by zero")
    }
    return a / b, nil
}

do main() {
    temp result, err = divide(10, 0)
    if err != nil {
        println("Failed: ${err.message}")
    } otherwise {
        println("Result: ${result}")
    }
}
Error CodeCondition
E7001Wrong number of arguments
E7003Argument is not a string

Quick Reference

FunctionDescriptionExample
input()Read line from stdintemp name = input()
read_int()Read integer from stdintemp num = read_int()
len(x)Length of string, array, or maplen("hello")5
range(start, end)Number sequence for loopsrange(0, 5)0,1,2,3,4
range(start, end, step)Number sequence with steprange(0, 10, 2)0,2,4,6,8
typeof(x)Type name as stringtypeof(42)"int"
copy(x)Deep copy of a valuecopy(myStruct) → independent copy
int(x)Convert to integerint("42")42
float(x)Convert to floatfloat(42)42.0
string(x)Convert to stringstring(42)"42"
char(x)Convert int to characterchar(65)'A'
error(msg)Create user-defined errorerror("invalid input")Error