2.35. Built-in Functions
Daslang built-in functions fall into two categories:
Intrinsic functions — compiled directly into the AST as dedicated expression nodes (e.g.
invoke,assert,debug). They are documented in this section.Standard library functions — defined in
builtin.dasand other standard modules that are always available. These include container operations (push,sort,find_index), iterator helpers (each,next), clone, lock, and serialization functions. They are documented in the standard library reference (see Built-in functions reference).
This page covers both groups, organized by category.
2.35.1. Invocation
- invoke(block_or_function, arguments)
Calls a block, lambda, or function pointer with the provided arguments:
def apply(f : block<(x : int) : int>; value : int) : int { return invoke(f, value) }
Blocks, lambdas, and function pointers can also be called using regular call syntax. The compiler will expand
f(value)intoinvoke(f, value):def apply(f : block<(x : int) : int>; value : int) : int { return f(value) // equivalent to invoke(f, value) }
2.35.2. Assertions
- assert(x, str)
Triggers an application-defined assert if
xisfalse.assertmay be removed in release builds, so the expressionxmust have no side effects — the compiler will reject it otherwise:assert(index >= 0, "index must be non-negative")
- verify(x, str)
Triggers an application-defined assert if
xisfalse. Unlikeassert,verifyis never removed from release builds (it generatesDAS_VERIFYin C++ rather thanDAS_ASSERT). Additionally, the expressionxis allowed to have side effects:verify(initialize_system(), "initialization failed")
- static_assert(x, str)
Causes a compile-time error if
xisfalse.xmust be a compile-time constant.static_assertexpressions are removed from the compiled program:static_assert(typeinfo(is_pod type<Foo>), "Foo must be POD")
- concept_assert(x, str)
Similar to
static_assert, but the error is reported one level above in the call stack. This is useful for reporting type-contract violations in generic functions:def sort_array(var a : auto(TT)[]) { concept_assert(typeinfo(is_numeric type<TT>), "sort_array requires numeric type") sort(a) }
2.35.3. Debug
- debug(x, str)
Prints the string
strand the value ofx(similar toprint), then returnsx. This makes it suitable for debugging inside expressions:let mad = debug(x, "x") * debug(y, "y") + debug(z, "z")
- print(str)
Outputs the string
strto the standard output.printonly accepts strings — to print other types, use string interpolation:print("hello\n") // ok print("{13}\n") // ok, integer is interpolated into the string // print(13) // error: print expects a string
2.35.4. Panic
- panic(str)
Terminates execution with the given error message. Panic can be caught with a
try/recoverblock, but unlike C++ exceptions,panicis intended for fatal errors only. Recovery may have side effects, and not everything on the stack is guaranteed to recover properly:try { panic("something went wrong") } recover { print("recovered from panic\n") }
2.35.5. Memory & Type Utilities
- addr(x)
Returns a pointer to the value
x. This is an unsafe operation:unsafe { var a = 42 var p = addr(a) // p is int? }
- intptr(p)
Converts a pointer (raw or smart) to a
uint64integer value representing its address:let address = intptr(some_ptr)
- typeinfo(trait expression)
Provides compile-time type information about an expression or a
type<T>argument. Used extensively in generic programming:typeinfo(sizeof type<float3>) // 12 typeinfo(typename type<int>) // "int" typeinfo(is_pod type<int>) // true typeinfo(has_field<x> myStruct) // true if myStruct has field x
(see Generic Programming for a full list of typeinfo traits).
2.35.6. Array Operations
The following operations are defined in builtin.das and are always available.
2.35.6.1. Resize & Reserve
- resize(var arr : array<T>; new_size : int)
Resizes the array to
new_sizeelements. New elements are zero-initialized.
- resize_and_init(var arr : array<T>; new_size : int)
Resizes the array and default-initializes all new elements using the element type’s default constructor.
- resize_no_init(var arr : array<T>; new_size : int)
Resizes without initializing new elements. Only valid for POD/raw element types.
- reserve(var arr : array<T>; capacity : int)
Pre-allocates memory for at least
capacityelements without changing the array length.
2.35.6.2. Push & Emplace
- push(var arr : array<T>; value : T [; at : int])
Inserts a copy of
valueinto the array at indexat(or at the end ifatis omitted). Also accepts anotherarray<T>or a fixed-sizeT[]to push all elements at once:var a : array<int> push(a, 1) push(a, 2, 0) // insert 2 at beginning push(a, fixed_array(3, 4, 5)) // push three elements
- push_clone(var arr : array<T>; value [; at : int])
Clones
value(deep copy) and inserts the clone into the array.
- emplace(var arr : array<T>; var value : T& [; at : int])
Move-inserts
valueinto the array, zeroing the source. Preferred for types that own resources (e.g., other arrays, tables, smart pointers):var inner : array<int> push(inner, 1) var outer : array<array<int>> emplace(outer, inner) // inner is now empty
- emplace_new(var arr : array<smart_ptr<T>>; var value : smart_ptr<T>)
Move-inserts a smart pointer into the back of the array.
2.35.6.3. Remove & Erase
- erase(var arr : array<T>; at : int [; count : int])
Removes the element at index
at(orcountelements starting atat).
- erase_if(var arr : array<T>; blk : block<(element : T) : bool>)
Removes all elements for which
blkreturnstrue:erase_if(arr) $(x) { return x < 0 }
- remove_value(var arr : array<T>; value : T) : bool()
Removes the first occurrence of
value. Returnstrueif an element was removed.
- pop(var arr : array<T>)
Removes the last element of the array.
2.35.6.4. Access & Search
- back(var arr : array<T>) : T&()
Returns a reference to the last element. Panics if the array is empty.
- empty(arr : array<T>) : bool()
Returns
trueif the array has no elements.
- length(arr : T[]) : int()
Returns the compile-time dimension of a fixed-size array.
- find_index(arr; value : T) : int()
Returns the index of the first element equal to
value, or-1if not found. Works on dynamic arrays, fixed-size arrays, and iterators.
- find_index_if(arr; blk : block<(element : T) : bool>) : int()
Returns the index of the first element satisfying
blk, or-1if not found.
- has_value(arr; value) : bool()
Returns
trueif any element equalsvalue. Works on any iterable.
- subarray(arr; r : range) : array<T>()
Returns a new array containing elements in the specified range.
2.35.6.5. Sorting
- sort(var arr)
Sorts the array in ascending order using the default
<operator:var a <- array(3, 1, 2) sort(a) // a is now [1, 2, 3]
- sort(var arr; cmp : block<(x, y : T) : bool>)
Sorts the array using a custom comparator. The comparator should return
trueifxshould come beforey:sort(arr) $(a, b) { return a > b } // descending order
2.35.6.6. Swap
- swap(var a, b : T&)
Swaps two values using move semantics through a temporary.
2.35.7. Table Operations
2.35.7.1. Lookup
- key_exists(tab : table<K;V>; key : K) : bool()
Returns
trueifkeyexists in the table.
- get(tab : table<K;V>; key : K; blk : block<(value : V&)>)
Looks up
keyin the table. If found, the table is locked andblkis invoked with a reference to the value. Returnstrueif the key was found:get(tab, "key") $(value) { print("found: {value}\n") }
- get_value(var tab : table<K;V>; key : K) : V()
Returns a copy of the value at
key. Only works for copyable types.
- clone_value(var tab : table<K; smart_ptr<V>>; key : K) : smart_ptr<V>()
Clones the smart pointer value at
key.
2.35.7.2. Insert & Emplace
- insert(var tab : table<K;V>; key : K; value : V)
Inserts a key-value pair. For key-only tables (sets), only the key is needed:
var seen : table<string> insert(seen, "hello")
- insert_clone(var tab : table<K;V>; key : K; value : V)
Clones
valueand inserts the clone into the table.
- emplace(var tab : table<K;V>; key : K; var value : V&)
Move-inserts
valueinto the table atkey.
- insert_default(var tab : table<K;V>; key : K [; value])
Inserts
value(or a default-constructed value) only ifkeydoes not already exist.
- emplace_default(var tab : table<K;V>; key : K)
Emplaces a default value only if
keydoes not already exist.
2.35.7.3. Remove & Clear
- erase(var tab : table<K;V>; key : K) : bool()
Removes the entry at
key. Returnstrueif the key was found and removed.
- clear(var tab : table<K;V>)
Removes all entries from the table.
2.35.7.4. Iteration
- keys(tab : table<K;V>) : iterator<K>()
Returns an iterator over all keys in the table.
- values(tab : table<K;V>) : iterator<V&>()
Returns an iterator over all values in the table (mutable or const depending on the table).
- empty(tab : table<K;V>) : bool()
Returns
trueif the table has no entries.
2.35.8. Iterator Operations
- each(iterable)
Creates an iterator from a range, array, fixed-size array, string, or lambda:
for (x in each(my_range)) { print("{x}\n") }
Overloads exist for
range,urange,range64,urange64,string,T[],array<T>, andlambda.
- each_enum(tt : T)
Creates an iterator over all values of an enumeration type.
Deprecated since version Use: the built-in enumeration iteration instead.
- next(var it : iterator<T>; var value : T&) : bool()
Advances the iterator and stores the current value in
value. Returnsfalsewhen the iterator is exhausted.
- nothing(var it : iterator<T>) : iterator<T>()
Returns an empty (nil) iterator.
- iter_range(container) : range()
Returns
range(0, length(container))— useful for index-based iteration.
2.35.9. Conversion Functions
- to_array(source) : array<T>()
Converts a fixed-size array or iterator to a dynamic
array<T>:let fixed = fixed_array(1, 2, 3) var dynamic <- to_array(fixed)
- to_array_move(var source) : array<T>()
Moves elements from the source into a new dynamic array.
- to_table(source) : table<K;V>()
Converts a fixed-size array of tuples to a
table<K;V>, or a fixed-size array of keys to a key-only table (set):var tab <- to_table(fixed_array(("one", 1), ("two", 2)))
- to_table_move(var source) : table<K;V>()
Moves elements from the source into a new table.
2.35.10. Clone
- clone(src) : T()
Creates a deep copy of any value. For arrays and tables, all elements are cloned recursively:
var a <- [1, 2, 3] var b := a // equivalent to: clone(b, a)
- clone_dim(var dst; src)
Clones a fixed-size array into another of the same dimension.
(see Clone for full cloning rules).
2.35.11. Lock Operations
- lock(container; blk)
Locks an array or table, invokes
blkwith a temporary handled reference, then unlocks. While locked, the container cannot be resized or modified structurally:lock(my_table) $(t) { for (key in keys(t)) { print("{key}\n") } }
- lock_forever(var tab : table<K;V>) : table#()
Locks the table permanently and returns a handled (temporary) reference. This is useful for read-only lookup tables.
- lock_data(var arr : array<T>; blk : block<(var p : T?#; s : int)>)
Locks the array and provides raw pointer access to the underlying data along with its length.
2.35.12. Serialization
- binary_save(obj; blk : block<(data : array<uint8>#)>)
Serializes a reference-type object to a binary representation and invokes
blkwith the resulting byte array.
- binary_load(var obj; data : array<uint8>)
Deserializes a reference-type object from binary data.
2.35.13. Smart Pointer
- get_ptr(src : smart_ptr<T>) : T?()
Extracts a raw pointer from a smart pointer.
- get_const_ptr(src : smart_ptr<T>) : T? const()
Extracts a const raw pointer from a smart pointer.
- add_ptr_ref(src : smart_ptr<T>) : smart_ptr<T>()
Increments the reference count and returns a new smart pointer.
2.35.14. Memory Mapping
- map_to_array(data : void?; len : int; blk)
Maps raw memory to a temporary mutable array view. This is an unsafe operation.
- map_to_ro_array(data : void?; len : int; blk)
Maps raw memory to a temporary read-only array view. This is an unsafe operation.
2.35.15. Vector Construction
Helper functions for constructing vector types from individual components:
let v2 = float2(1.0, 2.0)
let v3 = float3(1.0, 2.0, 3.0)
let v4 = float4(1.0, 2.0, 3.0, 4.0)
let i2 = int2(1, 2)
let i3 = int3(1, 2, 3)
let i4 = int4(1, 2, 3, 4)
let u2 = uint2(1u, 2u)
let u3 = uint3(1u, 2u, 3u)
let u4 = uint4(1u, 2u, 3u, 4u)
These accept any numeric arguments and convert them to the appropriate element type.