Skip to main content
Klar

Built-in Functions

Klar provides built-in functions that are available without imports.

Output Functions

print

Print a string without a trailing newline.

print("Hello ")
print("World!")
// Output: Hello World!

Signature: fn print(s: string)

println

Print a string with a trailing newline.

println("Hello, World!")
// Output: Hello, World!\n

Signature: fn println(s: string)

String Interpolation

Both print and println support string interpolation:

let name: string = "Alice"
let age: i32 = 30
println("Name: {name}, Age: {age}")
// Output: Name: Alice, Age: 30

Input Functions

readline

Read a line from standard input.

print("Enter your name: ")
let name: string = readline()
println("Hello, {name}!")

Signature: fn readline() -> string

Assertion Functions

assert

Assert that a condition is true. Panics if false.

let x: i32 = 10
assert(x > 0)           // OK
assert(x > 100)         // Panic: assertion failed

Signature: fn assert(condition: bool)

assert_eq

Assert that two values are equal. Panics if not equal.

let a: i32 = 42
let b: i32 = 42
assert_eq(a, b)         // OK
assert_eq(a, 100)       // Panic: assertion failed: 42 != 100

Signature: fn assert_eq#[T: Eq](left: T, right: T)

Debugging Functions

dbg

Print a value with its expression for debugging. Returns the value.

let x: i32 = 10
let y: i32 = dbg(x + 5)  // Prints: [dbg] x + 5 = 15
// y is 15

Signature: fn dbg#[T](value: T) -> T

panic

Terminate the program with an error message.

fn divide(a: i32, b: i32) -> i32 {
    if b == 0 {
        panic("division by zero")
    }
    return a / b
}

Signature: fn panic(message: string) -> !

Type Information

len

Get the length of a collection or string.

let s: string = "hello"
let n: i32 = len(s)  // 5

let arr: [i32; 3] = [1, 2, 3]
let m: i32 = len(arr)  // 3

Signature: fn len#[T](collection: T) -> i32

Most collections also have a .len() method:

let length: i32 = "hello".len()  // 5

type_name

Get the name of a type as a string (runtime version).

let x: i32 = 42
println(type_name(x))  // "i32"

struct Point { x: i32, y: i32 }
let p: Point = Point { x: 1, y: 2 }
println(type_name(p))  // "Point"

Signature: fn type_name#[T](value: T) -> string

Mathematical Functions

sqrt

Square root (for floating point types).

let x: f64 = 16.0
let root: f64 = sqrt(x)  // 4.0

Signature: fn sqrt(x: f64) -> f64

abs

Absolute value.

let x: i32 = -42
let a: i32 = abs(x)  // 42

let y: f64 = -3.14
let b: f64 = abs(y)  // 3.14

Signatures:

  • fn abs(x: i32) -> i32
  • fn abs(x: f64) -> f64

min / max

Minimum and maximum of two values.

let a: i32 = 10
let b: i32 = 20

let smaller: i32 = min(a, b)  // 10
let larger: i32 = max(a, b)   // 20

Signatures:

  • fn min#[T: Ordered](a: T, b: T) -> T
  • fn max#[T: Ordered](a: T, b: T) -> T

Memory Functions

drop

Explicitly drop a value (call its Drop implementation).

var list: List#[i32] = List.new#[i32]()
list.push(1)
list.push(2)
drop(list)  // Explicitly free memory

Signature: fn drop#[T](value: T)

Note: Values are automatically dropped when they go out of scope. Use drop only when you need to free resources early.

Collection Constructors

Some / None

Create optional values.

let present: ?i32 = Some(42)
let absent: ?i32 = None

Ok / Err

Create result values.

let success: Result#[i32, string] = Ok(42)
let failure: Result#[i32, string] = Err("error message")

FFI Pointer Functions

These functions work with C pointers for FFI. Most require unsafe blocks.

is_null

Check if a nullable pointer is null. Safe to call without unsafe.

let ptr: COptPtr#[i32] = get_some_pointer()
if is_null(ptr) {
    println("Pointer is null")
}

Signature: fn is_null#[T](ptr: COptPtr#[T]) -> bool

unwrap_ptr

Convert a nullable pointer to a non-null pointer. Panics if null.

unsafe {
    let non_null: CPtr#[i32] = unwrap_ptr(ptr)
}

Signature: fn unwrap_ptr#[T](ptr: COptPtr#[T]) -> CPtr#[T] (unsafe)

read

Read the value at a pointer location.

unsafe {
    let value: i32 = read(ptr)
}

Signature: fn read#[T](ptr: CPtr#[T]) -> T (unsafe)

write

Write a value to a pointer location.

unsafe {
    write(ptr, 42)
}

Signature: fn write#[T](ptr: CPtr#[T], value: T) -> void (unsafe)

offset

Perform pointer arithmetic.

unsafe {
    let next: CPtr#[i32] = offset(ptr, 1)  // Move forward by 1 element
    let prev: CPtr#[i32] = offset(ptr, -1) // Move backward by 1 element
}

Signature: fn offset#[T](ptr: CPtr#[T], count: isize) -> CPtr#[T] (unsafe)

ref_to_ptr

Convert a reference to a raw pointer.

var x: i32 = 42
unsafe {
    let ptr: CPtr#[i32] = ref_to_ptr(ref x)
}

Signature: fn ref_to_ptr#[T](r: ref T) -> CPtr#[T] (unsafe)

ptr_cast

Cast a pointer to a different type.

unsafe {
    let byte_ptr: CPtr#[u8] = ptr_cast#[u8](int_ptr)
}

Signature: fn ptr_cast#[U, T](ptr: CPtr#[T]) -> CPtr#[U] (unsafe)

Comptime Builtins

These are available only in comptime contexts:

FunctionDescription
@typeName#[T]Get type name at compile time
@typeInfo#[T]Get type metadata
@hasField#[T](name)Check if struct has field
@fields#[T]Get struct field information
@assert(cond, msg)Compile-time assertion
@compileError(msg)Emit compile error
@repeat(value, n)Create array with repeated value

See Comptime for details.

Summary Table

FunctionDescriptionReturn Type
print(s)Print without newlinevoid
println(s)Print with newlinevoid
readline()Read line from stdinstring
assert(cond)Assert conditionvoid
assert_eq(a, b)Assert equalityvoid
dbg(value)Debug printT
panic(msg)Terminate program!
len(coll)Get lengthi32
type_name(val)Get type namestring
sqrt(x)Square rootf64
abs(x)Absolute valueT
min(a, b)MinimumT
max(a, b)MaximumT
drop(val)Explicit dropvoid
Some(val)Create Some?T
NoneNone value?T
Ok(val)Create OkResult#[T, E]
Err(val)Create ErrResult#[T, E]

FFI Functions (require unsafe)

FunctionDescriptionReturn Type
is_null(ptr)Check if null (safe)bool
unwrap_ptr(ptr)Unwrap nullable pointerCPtr#[T]
read(ptr)Read from pointerT
write(ptr, val)Write to pointervoid
offset(ptr, n)Pointer arithmeticCPtr#[T]
ref_to_ptr(ref)Reference to pointerCPtr#[T]
ptr_cast#[U](ptr)Cast pointer typeCPtr#[U]

Next Steps