Nitpick Generics

Nitpick supports zero-cost generic programming via monomorphization. Generic type parameters are declared using <T> syntax.

1. Generic Functions and Structs

You can define functions and data structures that operate over generic types.

// Generic Struct
struct:Container<T> = {
    T:value;
};

// Generic Function
func:extract_value<T> = T(Container<T>:c) {
    pass c.value;
};

2. Generic Call Syntax (Implicit)

Generic functions can be instantiated without the turbofish operator in most contexts using standard function call syntax: func<T>(args...). The compiler disambiguates this from the < comparison operator automatically.

int32:val = extract_value<int32>(my_container) ? 0i32;

3. Turbofish Instantiation (::<T>)

When implicit syntax cannot be used (e.g. ambiguity or missing context), you can fall back to explicitly providing the type using the turbofish operator ::<T>.

int32:val = extract_value::<int32>(my_container) ? 0i32;

4. Nested Generics

When nesting generic types, the compiler automatically splits contiguous closing brackets >>. You do not need to add spaces between them.

Handle<Node<int32>>:my_handle; // Valid

5. The Type Keyword

The Type keyword is used in advanced generic constraints (often in conjunction with Type checking or existential configurations).

// Ensures T strictly conforms to an expected Type layout
// (Often used in bounds checking and trait bounds)

6. Arena UFCS Chained Calls in Generic Structs

Generic structs may contain arena<T> fields. These support a UFCS (Uniform Function Call Syntax) chained call pattern for arena operations:

struct:Header<T> = {
    arena<Node<T>>:node_arena;
};

// Direct struct variable: field access via '.'
Header<int32>:hdr;
Handle<Node<int32>>:h = hdr.node_arena.alloc(my_node);

// Pointer to struct: field access via '->'  (supported as of v0.61.85)
Header<int32>->:ptr = cast_unchecked<Header<int32>->>(raw_address);
Handle<Node<int32>>:h2 = ptr->node_arena.alloc(my_node);

The compiler (GEN-CHAIN-001, v0.61.83+, improved v0.61.85) handles both direct struct variables and pointer-dereferenced struct variables for arena UFCS dispatch.

Note: When the generic function is monomorphized, local variable type names used for arena field lookup must exactly match the struct registration. The compiler strips trailing -> from pointer-typed variables automatically when resolving struct fields.