Type System Overview
Nominal types, managed and unmanaged values, inference, assignability, and conversion.
Contents
Haze is statically typed and nominal. Every declared type has an identity, not merely a shape, so two records with identical fields are different types and cannot be substituted for one another. The type system is small enough to describe in tables, and the rules below apply throughout the reference.
Type Categories#
| Category | Examples | Managed | Nominal |
|---|---|---|---|
| Scalar | i8 through i64, u8 through u64, f64, bool, rune, enum |
No | Built-in or declared |
| Text | str, cstr |
str is immutable and runtime-owned; cstr is raw |
Built-in |
| Buffer | bytes |
Yes | Built-in |
| Pointer | ptr |
Yes as a root, but points to unmanaged memory | Built-in |
| Collection | List[T] |
Yes | Built-in generic |
| Product | record | Yes | Yes |
| Sum | sum type, Option[T], Result[T, E] |
Yes | Yes |
| Function | fn(T) U |
Yes when it is a closure | Structural |
| Refinement | type Positive = i32 : invariant ... |
Follows the base | Yes |
void is a type only in return position. A parameter cannot have type void, and a variable cannot have type void. Function values, lists, options, and results are parameterized by other types; there are no higher-kinded types, associated types, or type classes beyond traits.
Type Syntax#
| Form | Meaning |
|---|---|
i32, bool, str |
A built-in scalar or text type |
List[T] |
A list whose elements have type T |
Option[T] |
A value that is Some(T) or None |
Result[T, E] |
A value that is Success(T) or Error(E) |
fn(T, U) R |
A function value taking T and U and returning R |
Name |
A declared record, sum type, enum, or refinement |
Name[A, B] |
An instantiation of a generic declared type |
T |
A type parameter inside a generic declaration |
There are no transparent type aliases. Writing type Foo = Bar is a refinement declaration and requires at least one : invariant clause; it does not create an alternate spelling for Bar.
Inference#
The compiler infers local variable types from initializers, generic function type arguments from arguments, generic constructor arguments from fields, and lambda parameter types from the expected function type.
const n = 42 // i32
const values = [1, 2, 3] // List[i32]
const pair = Pair { first = 1, second = "one" }
const twice = fn(x) => x * 2 // inferred from the context where it is used
An empty list and None have no element type of their own. They need a surrounding declaration or a parameter type that supplies one.
var values List[i32] = [] // element type comes from the annotation
const absent Option[str] = None
When inference cannot determine a type parameter, the compiler reports an error rather than choosing Unknown silently. Annotate the binding, add a parameter type to the lambda, or pass a value that makes the type unambiguous.
Assignability#
Assignability is narrower than the set of conversions. The table lists the allowed directions.
| From | To | Allowed | Note |
|---|---|---|---|
| Same type | Same type | Yes | Identity |
| Narrower integer of the same signedness | Wider integer | Implicitly | i8 to i32, u8 to u64 |
| Wider integer | Narrower integer | Only with trunc |
Explicit narrowing |
| Signed integer | Unsigned integer | Only with explicit conversion | Avoid implicit mixed-sign code |
| Integer | f64 |
With cast |
Numeric conversion |
f64 |
Integer | With cast |
Fractional part is discarded |
Refinement R |
Its base type | Implicitly | The value satisfies the weaker type |
| Base type | Refinement R |
Only with a check | Constants may be verified; runtime values use checked construction |
| Generic instantiation with compatible arguments | Another instantiation | Yes | Arguments must be assignable |
| Any type | Unknown |
During inference | Not a user-facing type |
No other implicit conversions exist. In particular, there is no implicit conversion from bool to an integer, from str to bytes, from a record to a structurally identical record, or from a sum type to one of its variants.
Managed and Unmanaged Values#
Managed values live on the Haze heap and are traced by the collector. They include records, sum values, Option, Result, List, bytes, ptr, and closures. Unmanaged values include scalars, enums, str, cstr, and extern records. The distinction matters at the FFI boundary and when reasoning about aliasing, but ordinary Haze code should not need to manage either category manually.
A managed value is passed by reference. Assigning it to another binding copies the reference, not the object. A scalar is copied. The Values and Mutability chapter explains the practical consequences for mutation and function arguments.
Nominal Identity#
Records, sums, enums, and refinements are nominal. Two declarations with the same fields are distinct types. A function that takes Account does not accept a structurally identical Customer, even if both have an id and a balance field. This prevents accidental mixing of domain concepts and makes the type name carry meaning.
type Account {
id i32
balance i64
}
type Customer {
id i32
balance i64
}
fn deposit(account Account, amount i64) Account => Account {
id = account.id,
balance = account.balance + amount,
}
The explicit construction also makes the domain transition visible. A conversion between the two types should be a named function with a contract, not an implicit coercion.
Generic Instantiations#
A generic type is not a type until it is instantiated. Box alone is a declaration; Box[i32] is a type. The compiler instantiates generic functions and types as needed and checks each instantiation against its bounds.
type Box[T] {
value T
}
const integers Box[i32] = Box[i32] { value = 1 }
const strings Box[str] = Box[str] { value = "one" }
Instantiation is part of the type's identity. Box[i32] and Box[str] are different types. A trait implementation may target a specific instantiation, as in impl Box[i64] of Sum, in which case the methods are available only for that instantiation.
Refinements#
A refinement type is a distinct type with the same representation as its base, plus one or more invariants. Values of the refinement satisfy the invariants by construction; values of the base do not automatically satisfy them.
type Positive = i32 : invariant self > 0
const n = Positive { 5 } // accepted at compile time
const bad = Positive { 0 } // compile-time error
The Refinement Types chapter covers composition, checked construction, and the rules for assignment. The Checked Construction chapter explains the runtime path for values that are not compile-time constants.
The Type of a Function#
A function type is written fn(parameters) result. A free function name may be used as a first-class value when its signature is concrete. A generic function and an extern function cannot be used as values because they do not have a single concrete signature.
fn apply(value i32, transform fn(i32) i32) i32 => transform(value)
fn double(n i32) i32 => n * 2
fn main() {
print(apply(3, double))
print(apply(3, fn(n) => n + 1))
}
Function types are structural: two function types with the same parameter and result types are the same type. There are no effects in the function type, so pure does not change assignability.