Type System Overview
Nominal types, managed values, inference, assignability, and conversion.
Contents
Haze is statically typed and nominal. Every declared type has an identity; two records with identical fields are different types.
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 |
Root value; 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. It cannot be used for a parameter, variable, list element, option payload, or generic argument. There are no higher-kinded types or associated types.
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. type Foo = Bar is a refinement declaration and requires an : invariant clause.
Inference#
The compiler infers local variable types from initializers, generic arguments from call 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
An empty list and None have no element type. They need a surrounding declaration or parameter type.
var values List[i32] = []
const absent Option[str] = None
When inference cannot determine a type parameter, the compiler reports an error. Annotate the binding, add a lambda parameter type, or pass a value that makes the type unambiguous.
Assignability#
| 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 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 | Another instantiation with compatible arguments | Yes | Arguments must be assignable |
| Any type | Unknown |
During inference | Not user-facing |
No other implicit conversions exist. There is no implicit conversion from bool to integer, str to bytes, a record to a structurally identical record, or a sum type to a variant.
Managed and Unmanaged Values#
Managed values are traced by the collector: records, sum values, Option, Result, List, bytes, ptr, and closures. Unmanaged values include scalars, enums, str, cstr, and extern records. A managed value is passed by reference. A scalar is copied. See Values and Mutability.
Nominal Identity#
Records, sums, enums, and refinements are nominal. A function that takes Account does not accept a structurally identical Customer.
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,
}
A conversion between two such types should be a named function with a contract.
Generic Instantiations#
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" }
Box[i32] and Box[str] are different types. A trait implementation may target a specific instantiation, as in impl Box[i64] of Sum.
Refinements#
A refinement type is a distinct type with the same representation as its base, plus invariants. Values of the refinement satisfy the invariants by construction; base values do not.
type Positive = i32 : invariant self > 0
const n = Positive { 5 } // accepted at compile time
const bad = Positive { 0 } // compile-time error
See Refinement Types and Checked Construction.
Function Types#
A function type is written fn(parameters) result. A free function name may be used as a value when its signature is concrete. Generic and extern functions cannot be used as values.
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. pure does not change assignability.