Templates
Template Types
Template types are parameterized over one or more type arguments:
type Box<T>{ T value;
Box(T value) { this.value = value; }
T Get() this.value;}
Box<int> b = Box<int>(42);Template Functions
Functions can also be parameterized:
T Identity<T>(T x) x;
int Max<T>(T a, T b) a > b ? a : b;
int result = Max<int>(10, 20);Value Templates
Use typed parameters for compile-time values:
type FixedArray<int N>{ int size;}
int GetSize<int N>() N;int result = GetSize<100>(); // 100Constraints with requires(...)
Use requires(...) to restrict template parameters. Parentheses are mandatory — a bare requires cond is a parse error. A space between requires and ( is optional.
Single Condition
type PositiveBuffer<int N> requires(N > 0){ int size;}
type AlignedBuffer<int N> requires(N % 16 == 0){ int size;}Multiple Conditions
Pass a comma-separated list inside the parens:
type Bounded<int N> requires(N > 0, N < 1024){ int val;}
// Combine constraints with trusted contexttype SecureBuffer<int N> requires(N > 0, caller: trusted){ int size;}Each comma-separated item is an independent check — it can be a constraint expression or a keyword like trusted.
Non-Template requires(...) Clauses
requires(...) also attaches constraints to types, methods, and properties outside templates. The recognised keyword forms are:
| Form | Where it goes | Effect |
|---|---|---|
requires(caller: trusted) | type body, method, property, free function | Caller must itself be requires(caller: trusted). Required to call FFI / extern functions. |
requires(this: gc) | per-method (and per-property; propagates to accessors) | Receiver must be GC-allocated (T! / T?). Stack values and raw pointers are rejected at the call site and at delegate-binding time. Use on methods that capture this past their own frame (JNI / OS / external callbacks) — GC rooting is the only allocation strategy with a usable lifetime guarantee. |
requires(new: gc | value | heap) | type body only | Restricts allowed allocation strategies. gc allows T! / T? and new T(); value allows stack / embedded values; heap allows raw new* T(). Bits combine with |. Inheritance rule: a derived type’s bit set must be a subset of the parent’s — narrowing is allowed, widening is a parse error. |
// GC-rooted receiver (delegate / JNI handler)type Listener!{ void OnEvent(int code) requires(this: gc) { /* may capture `this` */ }}
// Allocation strategy lock-downtype View requires(new: gc) { /* ... */ } // GC onlytype Point requires(new: value) { /* ... */ } // stack onlytype Buffer requires(new: gc | value) { /* ... */ } // either, no raw heaptype Raw requires(new: heap) { /* ... */ } // raw `new*` onlynew: pointer is intentionally absent — new: describes instantiation strategy, and raw pointer use (T*) stays gated by unsafe.
Type Constraints with T.Inherits
Constrain type parameters to inherit from a specific type:
interface IComparable{ int Compare();}
type SortedList<T> requires(T.Inherits(IComparable)){ T item;}
// Multiple checks including type constrainttype SafeSortedList<T, int N> requires(T.Inherits(IComparable), N > 0){ T item;}Multiple Type Parameters
type Pair<K, V>{ K first; V second;
Pair(K first, V second) { this.first = first; this.second = second; }}Default Type Parameters
type Optional<T = int>{ T value; int hasValue;}Variadic Templates
Variadic templates accept a variable number of type or value arguments using the ... suffix. At most one variadic parameter is allowed, it must be the last primary parameter, and zero arguments are allowed.
Variadic Type Parameters
type Tuple<Types...>{ [Types...] data;}
// Mixed: regular + variadictype Pair<First, Rest...>{ First head;}Variadic Value Parameters
int sum<int Ns...>(){ return (Ns + ...);}
int result = sum<1, 2, 3>(); // 6sizeof…(Pack)
Returns the number of elements in a parameter pack as a compile-time constant:
int count<Types...>() sizeof...(Types);
count<int, float, string>() // 3count() // 0Pack Expansion
Use Types... or Ns... to expand a pack in type or expression contexts:
// Forward type pack to another templateint inner<Types...>() sizeof...(Types);int outer<Types...>() inner<Types...>();Function Parameter Packs
A variadic type parameter used as a function parameter type followed by ... expands into individual parameters. Use args... to forward them to another call:
void target(int a, int b) { }
void forward<Args...>(Args... args){ target(args...);}
forward<int, int>(10, 20);Pack Element Access
Access individual elements by compile-time index:
int FirstArg<Args...>(Args... args) args[0];
int nth<int Ns...>() Ns[1];
nth<10, 20, 30>() // 20Variadic For-Each
Iterate over pack elements with unrolled for loops:
int SumAll<Args...>(Args... args){ int total = 0; for (x : args...) { total = total + x; } return total;}Fold Expressions
Fold expressions reduce a value pack with a binary operator:
// Unary right fold: a1 + (a2 + a3)int sum<int Ns...>() (Ns + ...);
// Unary left fold: (a1 + a2) + a3int sum<int Ns...>() (... + Ns);
// Binary fold with init (safe for empty packs)int sum<int Ns...>() (0 + ... + Ns);Supported operators: +, -, *, /, %, &, |, ^, &&, ||, ==, !=, <, >, <=, >=.
Constraints with sizeof…
int first<int Ns...>() requires(sizeof...(Ns) > 0){ return Ns[0];}Template Instantiation
Each unique combination of type arguments creates a distinct compiled type. Template bodies are compiled when first used with a given set of arguments.
Cross-Assembly Templates
Function and type templates round-trip through .asm boundaries. A template defined in one assembly can be imported and instantiated by a consumer assembly with no special syntax at the call site — Identity<int>(42) works the same whether Identity lives in the local assembly or in an imported one.
// In Library.asm:T Identity<T>(T x) x;
// In Consumer.asm (after `using Library`):int n = Identity<int>(42);string s = Identity<string>("hi");The template body source is captured during compilation, persisted into the producer’s .asm, and re-lexed at the consumer side when the import is processed. Value-parameter requires(...) clauses round-trip with the rest of the template, and template bodies can freely reference other imported symbols (other templates, non-template functions, or types from other assemblies). Identical instantiations across imports collapse to a single concrete entity in the instantiation cache.