Language Tour
A guided tour of Haze's values, types, contracts, generics, and modules.
Contents
This tour is a map, not a reference. Each section shows a representative fragment and explains the idea behind it; the detailed chapters follow the same order and fill in the rules.
Bindings and Functions#
Haze distinguishes a binding that may be reassigned (var) from one that may not (const). Function parameters are local bindings, and a function may return a value with => or with return inside a block.
fn add(a i32, b i32) i32 => a + b
fn sum_to(n i32) i32 {
var total = 0
for i in 0..n {
total += i
}
return total
}
Integer literals default to i32 when they fit and i64 otherwise. Wider integers are available from i8 to i64 and u8 to u64, alongside f64, bool, str, rune, bytes, cstr, and ptr. The Primitive Types chapter gives the ranges and conversion rules.
Records and Invariants#
A record groups named fields. A type may add invariants between its fields, and those invariants are checked at construction.
type Account {
id i32
balance i64
}
type Positive = i64 : invariant self > 0
type Transfer : invariant src.id != dst.id {
src Account
dst Account
}
A refinement type such as Positive is a distinct type with the same representation as its base. A value of the base type is not automatically a Positive; it must be built and checked. The Refinement Types and Checked Construction chapters explain the rules, including why arithmetic on a refinement produces the base type.
Contracts#
A function can constrain its inputs and its result. Clauses may be repeated inline, or a named contract may package a reusable set of clauses.
contract Funded(account Account, amount i64) {
pre amount > 0
pre amount <= account.balance
}
fn withdraw(account Account, amount i64) Account
: Funded(account, amount)
: post r => r.id == account.id
: post r => r.balance == account.balance - amount
=> Account { id = account.id, balance = account.balance - amount }
Contracts are evaluated at compile time when the arguments are constant. Otherwise they become runtime checks unless the program is built with -release. A failed runtime precondition or postcondition is a panic; only checked construction returns an error value. The Contracts chapter covers the evaluation model, and Loop Invariants covers the loop form.
Sum Types and Matching#
A sum type is a value that is exactly one of several variants. Variants may carry fields. Pattern matching binds those fields and selects a branch.
type Shape =
| Circle(radius f64)
| Rect(width f64, height f64)
fn area(shape Shape) f64 => match shape {
Circle(radius) => 3.14159 * radius * radius
Rect(width, height) => width * height
}
A wildcard arm uses _. A refinement can also be used as a pattern: n is Positive(p) tests the invariant and binds the value when it holds. Patterns are covered in Pattern Matching, and the built-in Option and Result types are covered in Option and Result.
Lists and Pipelines#
Lists are written with square brackets and have element type List[T]. The empty list needs a contextual element type, which a declaration annotation supplies. Lists support indexing, len, concatenation with +, and appending with += inside a var binding.
fn even_squares(xs List[i32]) List[i32] {
var out List[i32] = []
for x in xs {
if x % 2 == 0 {
out += [x * x]
}
}
return out
}
Free functions that take a list as their first parameter can be called with method syntax. The standard library's map, filter, and fold are ordinary generic functions, so a pipeline reads naturally.
const values = [1, 2, 3, 4, 5]
.map(fn(x) => x * 2)
.filter(fn(x) => x > 4)
The Lists chapter gives the full set of operations and the aliasing rules.
Generics#
A generic function declares type parameters in square brackets. The compiler infers them from the arguments, and a type parameter may carry a trait bound.
fn identity[T](value T) T => value
fn largest[T Ord](a T, b T) T {
if a.lt(b) {
return b
}
return a
}
Generic records and sum types declare their parameters after the type name, as in Pair[A, B]. Instantiations use the same bracket syntax in types and record literals, as in Pair[i32, str] { first = 1, second = "one" }. The Generics chapter covers inference and bounds.
Traits#
A trait declares a set of methods. An implementation provides those methods for a concrete type. Traits may have default methods, generic parameters, base traits, and contracts on their methods.
trait Eq {
pub fn eq(x, y Self) bool
pub fn neq(x, y Self) bool {
return !x.eq(y)
}
}
impl i32 of Eq {
pub fn eq(x, y Self) bool => x == y
}
The Self type stands for the implementing type. A bound such as T Ord makes the methods of Ord available on values of type T. The Traits chapter explains composition, defaults, and implementations for built-in and generic types.
Modules#
Every file is a module. A file imports another with use, optionally naming the symbols it wants. Only pub declarations cross a module boundary.
use std/seq {map, filter}
use orders {Order, total}
fn main() {
const orders = [Order { id = 1, qty = 2, price = 30 }]
print(orders.map(total))
}
Module paths resolve relative to the importing file, and core.hz is the conventional file name for a directory module. The Modules chapter covers visibility, wildcard imports, relative paths, and the standard library search rule.
Defer#
defer registers a call or block to run when the enclosing scope exits. Defers run in last-in, first-out order, once per loop iteration, and during returns, breaks, continues, and ? propagation.
fn copy(input str, output str) Result[bool, str] {
defer print("copy finished")
if input == output {
return Error("same file")
}
return Success(true)
}
The Defer chapter covers scope behaviour, evaluation time, and the restrictions inside deferred code.
Memory and Runtime#
Managed values are reclaimed automatically. The compiler tracks roots, so closures and local references remain valid across allocation points. str and cstr are different: str is an immutable string value, while cstr is a raw C string used at the FFI boundary. The Memory and Garbage Collection chapter explains the model, and Extern and C FFI explains the boundary.
The Next Step#
The tour shows the vocabulary. The next chapters define the grammar and semantics precisely, and the Writing Idiomatic Haze Code chapter turns the vocabulary into a style. Readers who want the shortest path to writing good Haze should read Contracts First immediately after the contract chapters.