Built-in Functions
Klar provides built-in functions that are available without imports.
Output Functions
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!\nSignature: 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: 30Input 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 failedSignature: 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 != 100Signature: 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 15Signature: 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) // 3Signature: fn len#[T](collection: T) -> i32
Most collections also have a .len() method:
let length: i32 = "hello".len() // 5type_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.0Signature: 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.14Signatures:
fn abs(x: i32) -> i32fn 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) // 20Signatures:
fn min#[T: Ordered](a: T, b: T) -> Tfn 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 memorySignature: 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 = NoneOk / 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:
| Function | Description |
|---|---|
@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
| Function | Description | Return Type |
|---|---|---|
print(s) | Print without newline | void |
println(s) | Print with newline | void |
readline() | Read line from stdin | string |
assert(cond) | Assert condition | void |
assert_eq(a, b) | Assert equality | void |
dbg(value) | Debug print | T |
panic(msg) | Terminate program | ! |
len(coll) | Get length | i32 |
type_name(val) | Get type name | string |
sqrt(x) | Square root | f64 |
abs(x) | Absolute value | T |
min(a, b) | Minimum | T |
max(a, b) | Maximum | T |
drop(val) | Explicit drop | void |
Some(val) | Create Some | ?T |
None | None value | ?T |
Ok(val) | Create Ok | Result#[T, E] |
Err(val) | Create Err | Result#[T, E] |
FFI Functions (require unsafe)
| Function | Description | Return Type |
|---|---|---|
is_null(ptr) | Check if null (safe) | bool |
unwrap_ptr(ptr) | Unwrap nullable pointer | CPtr#[T] |
read(ptr) | Read from pointer | T |
write(ptr, val) | Write to pointer | void |
offset(ptr, n) | Pointer arithmetic | CPtr#[T] |
ref_to_ptr(ref) | Reference to pointer | CPtr#[T] |
ptr_cast#[U](ptr) | Cast pointer type | CPtr#[U] |
Next Steps
- Keywords - Reserved keywords
- Primitives - Built-in types
- Operators - All operators