Contents

Type parameters use square brackets. The compiler infers type arguments at most call sites.

Generic Functions#

Type parameters are declared after the function name and may be used in the parameter list, return type, and body.

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

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

Type arguments are inferred from the arguments. There is no identity[i32](value) syntax in expression position.

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 it cannot be inferred.

Bounds#

A type parameter may carry a trait bound. The bound makes the trait's methods available 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].

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)

Generic Records#

Type parameters are part of the type's identity; 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" }

Instantiations may nest.

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.

Generic Sum Types#

Constructors infer 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")
const nothing Maybe[i32] = Nothing

A recursive field is instantiated recursively: every node in a Tree[i32] is a Tree[i32].

Recursive Generic Functions#

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

The recursive call infers T from the argument.

Generic Types and Traits#

An implementation may target a specific 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 be generic and use its own type parameter.

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

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

An implementation for Trait[A] is selected by the receiver type and trait arguments, not a runtime tag. See Traits.

Instantiation#

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. There is no boxing of type parameters and no runtime type information.

A generic value must be known at compile time. An uncalled instantiation produces no specialization. A trait bound must be satisfied by a concrete implementation; there is no dynamic dispatch fallback.

Limits#

Generic enums and generic extern records are rejected. Contract declarations cannot declare type parameters. There are no default type arguments, associated types, higher-kinded parameters, or specialization attributes.

An unconstrained generic function is checked when instantiated. A bounded generic function is checked at its declaration against the bound. A type parameter name may not shadow a builtin type or another parameter in the same declaration.

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.

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

The body is well typed, but empty() cannot infer T. Haze does not provide explicit function type arguments at call sites. Prefer a parameter that determines the type, or provide named functions with concrete return types.

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