Contents

Option[T] represents a value that may be absent. Result[T, E] represents an operation that may fail. Neither has unwrap; the caller must match, test, or propagate the outcome.

Option#

Some(value) contains a value of type T; None contains nothing. None needs a contextual type when the element type cannot be inferred.

fn find_first_positive(values List[i32]) Option[i32] {
    for value in values {
        if value > 0 {
            return Some(value)
        }
    }
    return None
}
const absent Option[i32] = None
const present = Some(42)
const value = find_first_positive([-3, 0, 5, 7])
if value is Some(n) {
    print(n)
} else {
    print("no positive value")
}

match value {
    Some(n) => print(n)
    None => print("missing")
}

Option is a sum type, not a nullable pointer or sentinel.

Result#

Success(value) contains a value of type T; Error(reason) contains an error of type E. The error type is part of the signature.

type FsError =
    | NotFound(path str)
    | Denied(path str)

fn read_config(path str) Result[str, FsError] {
    if path == "" {
        return Error(NotFound(path))
    }
    return Success("configuration")
}
match read_config("config.haze") {
    Success(text) => print(text)
    Error(NotFound(path)) => print($"missing {path}")
    Error(Denied(path)) => print($"denied {path}")
}

A dedicated sum error type makes failure modes part of the function's contract. A string error is appropriate when the caller only needs a message.

Propagating with ?#

? unwraps a Result on success and returns the error from the enclosing function on failure.

fn load_twice(path str) Result[str, FsError] {
    const first = read_config(path)?
    const second = read_config(path)?
    return Success(first + second)
}

The enclosing function must return a Result with the same error type. ? does not convert between error types.

fn load_message(path str) Result[str, str] => match read_config(path) {
    Success(text) => Success(text)
    Error(reason) => match reason {
        NotFound(missing) => Error($"missing {missing}")
        Denied(_) => Error("permission denied")
    }
}

? is not defined for Option.

fn required(value Option[i32]) Result[i32, str] => match value {
    Some(n) => Success(n)
    None => Error("value is missing")
}

Checked Construction#

After a record literal, ? constructs a value and checks its invariants at runtime, returning an Error instead of aborting.

type Positive = i32 : invariant self > 0

fn make_positive(n i32) Result[Positive, str] => Success(Positive { n }?)

The enclosing function must return Result[_, str] when a runtime check is needed. The error is a string of the form invariant failed for 'T'. If the construction can be verified at compile time, ? produces a lint warning; if it is provably invalid, it is a compile error. See Checked Construction.

Error Types#

Error type Use when
str The caller only needs a message
Sum type Different failures need different recovery or messages
Record The error carries structured context such as a path, identifier, or cause
Option Absence is expected and there is no meaningful reason to report
type ParseError =
    | Empty
    | InvalidDigit(position i32, character u8)
    | Overflow

fn parse_i32(text str) Result[i32, ParseError] {
    if text.len == 0 {
        return Error(Empty)
    }
    var value = 0
    for i in 0..text.len {
        const digit = text[i]
        if digit < 48 || digit > 57 {
            return Error(InvalidDigit(i, digit))
        }
        value = value * 10 + cast[i32](digit - 48)
    }
    return Success(value)
}

No Unwrap#

There is no unwrap, expect, or implicit panic for a failed Result or empty Option. The payload is obtained through match or is. ? propagates failure to a caller that has declared it can fail.

Option or Result#

Use Option when absence is expected and there is nothing useful to say about why. Use Result when the caller needs a reason or different failures require different recovery.

fn lookup(values List[i32], index i32) Option[i32] => values.get(index)

fn require_at(values List[i32], index i32) Result[i32, str] => match lookup(values, index) {
    Some(value) => Success(value)
    None => Error($"index {index} is out of range")
}

Composition#

There is no monadic and_then in the core language. A helper expresses the same idea.

fn and_then[A, B, E](value Result[A, E], next fn(A) Result[B, E]) Result[B, E] =>
    match value {
        Success(a) => next(a)
        Error(reason) => Error(reason)
    }

Testing#

assert is available inside test blocks only.

test "parse_i32 rejects empty input" {
    assert parse_i32("") is Error(Empty)
}

test "parse_i32 accepts digits" {
    const result = parse_i32("123")
    if result is Success(value) {
        assert value == 123
    } else {
        assert false
    }
}
test "invalid digit reports its position" {
    const result = parse_i32("12x")
    if result is Error(reason) {
        match reason {
            InvalidDigit(position, character) => {
                assert position == 2
                assert character == 120
            }
            _ => {
                assert false
            }
        }
    } else {
        assert false
    }
}