Contents

Generics let a function or type be written once and used with different element types without losing type safety. Haze uses square brackets for type parameters and angle-free instantiation, and the compiler infers type arguments at most call sites.

Generic Functions#

A function declares type parameters after its name. The parameters may be used in the parameter list, the return type, and the body.

fn identity[T](value T) T => value

fn first[A, B](pair Pair[A, B]) A => pair.first

The compiler infers the type arguments from the actual arguments. A call does not spell them out; there is no identity[i32](value) syntax in expression position. This keeps calls readable and lets inference do the work.

const number = identity(42)          // T = i32
const text = identity("hello")       // T = str

If the arguments do not determine a type parameter, the compiler reports that the parameter cannot be inferred. Add an annotation, pass a value that determines the type, or make the parameter explicit in a function type.

Trait Bounds#

A type parameter may carry a trait bound. The bound makes the trait's methods available on values of that type and is checked at each instantiation.

trait Ord {
    fn lt(x, y Self) bool
}

fn maximum[T Ord](a T, b T) T {
    if a.lt(b) {
        return b
    }
    return a
}

A bound may be parameterized, as in T Convert[i64]. Multiple type parameters may each have their own bounds, and a function may have several bounds in total.

fn convert_and_compare[T Convert[i64]](value T) bool =>
    value.convert() > 0

fn both_ordered[T Ord, U Ord](a T, b T, c U, d U) bool =>
    a.lt(b) && c.lt(d)

The bound is checked when the function is instantiated, not when it is declared. A call with a type that does not implement the trait is rejected with an error that names the missing implementation.

Generic Records#

A record may declare type parameters. The parameters are part of the type's identity, so Box[i32] and Box[str] are different types.

type Box[T] {
    value T
}

type Pair[A, B] {
    first A
    second B
}

const integers Box[i32] = Box[i32] { value = 1 }
const strings Box[str] = Box[str] { value = "one" }

A generic record may be instantiated with another instantiation, including nested types.

const inner = Pair[i32, str] { first = 2, second = "two" }
const outer = Pair[Pair[i32, str], bool] { first = inner, second = true }

A generic record literal must write its type arguments explicitly. There is no expected-type inference for a bare Box { value = 1 }; write Box[i32] { value = 1 }.

Generic Sum Types#

A sum type may also be generic. Constructors infer the type arguments from their fields when possible.

type Maybe[T] = | Nothing | Just(value T)

type Tree[T] =
    | Leaf
    | Node(left Tree[T], value T, right Tree[T])

const number = Just(7)                    // Maybe[i32]
const text Maybe[str] = Just("hello")     // annotation supplies T
const nothing Maybe[i32] = Nothing        // no field to infer from

A generic sum with a recursive field is instantiated recursively. The declaration Node(left Tree[T], ...) means that every node in a Tree[i32] is also a Tree[i32].

Recursive Generic Functions#

A generic function may call itself recursively. The type arguments are inferred from the recursive call in the same way as from any other call.

fn length[T](tree Tree[T]) i32 => match tree {
    Leaf => 0
    Node(left, _, right) => 1 + length(left) + length(right)
}

The recursive call length(left) infers T from the type of left. No explicit instantiation is needed.

Generic Types and Traits#

A trait implementation may target a specific instantiation. The methods are then available only for that instantiation.

trait Sum[T] {
    fn sum(x, y T) T
}

impl Box[i64] of Sum {
    fn sum(x, y Self) Self => Self { value = x.value + y.value }
}

A trait may also be generic and use its own type parameter. The implementation supplies the argument.

trait Convert[A] {
    fn convert(x Self) A
}

impl Box[i64] of Convert[i64] {
    fn convert(x Self) i64 => x.value
}

The Traits chapter covers bounds, composition, and method resolution. The important generic rule is that an implementation for Trait[A] is selected by the receiver type and the trait arguments, not by a runtime tag.

Instantiation and Compilation#

Generic functions are instantiated per concrete type argument. The compiler creates a specialized function for each distinct instantiation and checks its body against the concrete types. This is the same model as monomorphization in other compiled languages; there is no boxing of type parameters and no runtime type information.

The practical consequences are that generic code has the performance of concrete code, generic values must be known at compile time, and a generic function that is never called with a particular type does not produce a specialization for that type. It also means that a trait bound must be satisfied by a concrete implementation; there is no dynamic dispatch fallback.

Limits#

The generic system is intentionally small. Generic enums and generic extern records are rejected. Contract declarations cannot declare type parameters, so a reusable contract over a generic type must use a concrete type or be expressed as inline clauses. There are no default type arguments, associated types, higher-kinded parameters, or specialization attributes.

An unconstrained generic function is checked when it is instantiated. A bounded generic function is checked at its declaration against the bound. This means an unused unconstrained generic can contain an error that is only reported when a call supplies the missing types.

A type parameter name may not shadow a builtin type or another parameter in the same declaration. The compiler reports the conflict at the declaration rather than at an instantiation.

Inference Failure#

Inference fails when a type parameter appears only in the return type, when a lambda parameter's type cannot be determined, or when an empty collection has no contextual element type. The remedy is to add the missing type at the nearest boundary.

fn empty[T]() List[T] {
    var out List[T] = []
    return out
}

The body of empty is well typed because the local declaration supplies the element type, but a call such as empty() cannot infer T. Haze does not provide explicit function type arguments, so a function whose type parameter appears only in the return type cannot be called usefully. Prefer a parameter that determines the type, or provide a family of named functions with concrete return types. This is also why the empty list requires a declaration annotation rather than a constructor.

Summary#

Feature Syntax Note
Function parameters fn f[T, U](...) Inferred at each call
Trait bound T Ord, T Convert[i64] Checked at instantiation
Generic record type Box[T] { ... } Instantiate as Box[i32]
Generic sum type Maybe[T] = | ... Constructor inference
Recursive generic Tree[T] fields Managed recursion
Trait implementation impl Box[i64] of Sum Per-instantiation methods
Explicit call arguments Not supported Use inference or annotate values
Generic contracts Not supported Use inline clauses or concrete types
Generic enums Not supported Use a sum type with payloads