Skip to content

Generics

Generics let a function or type work with more than one concrete type.

A generic type parameter is written inside <...>.

type Box:<T> := struct { value : T };

Generic functions use the same idea.

const id := fn <T> (x : T) -> T => {
x;
};

This id function works for any type because it returns the same kind of value it receives.

You can call a generic function with or without an explicit type argument depending on how well the type can be inferred.

let mut value : int = id:<int>(42);
value := id(50);

Generic types also accept explicit type arguments when they are used.

let item := Box:<int> { value : 42 };
print(item.value);

Generics are useful when you want reusable logic without copying the same function or type for int, str, float, and other types.