Checked Construction
The boundary between invariants, normal construction, and fallible construction.
Contents
A type with invariants cannot be built from arbitrary runtime values without a check. Haze separates the two cases with syntax: normal construction T { ... } has type T and must be provably valid, while checked construction T { ... }? may fail and returns an error to a Result-returning caller.
The Two Constructions#
Normal construction is allowed when the compiler can establish the invariants at compile time, or when the input already has the target type. Checked construction is used when the input is a runtime value whose validity cannot be decided statically.
type Positive = i32 : invariant self > 0
const good = Positive { 5 } // normal construction, checked at compile time
const bad = Positive { 0 } // compile-time error
fn make(n i32) Result[Positive, str] => Success(Positive { n }?)
The first construction is a compile-time constant, so the compiler evaluates the invariant and accepts it. The second is a constant that violates the invariant, so it is rejected before code generation. The third uses a runtime parameter, so it requires checked construction.
The Decision Table#
| Construction | Input state | Result |
|---|---|---|
T { ... } |
All inputs are compile-time constants and invariants hold | Accepted, type T |
T { ... } |
A constant provably violates an invariant | Compile error |
T { ... } |
Any input is not compile-time known and the value is not already of type T |
Compile error, with a hint to use ? |
T { ... } |
The value already has type T |
Accepted, no re-check |
T { ... }? |
Compiler can verify the construction | Warning: redundant check; type T |
T { ... }? |
Known to violate an invariant | Compile error, even with ? |
T { ... }? |
Runtime check needed, enclosing function returns Result[_, str] |
Type T on success; Error on failure |
T { ... }? |
Runtime check needed, enclosing function does not return Result[_, str] |
Compile error |
The requirement is specifically Result[_, str]. The error type must be exactly str, because the runtime failure message is a string.
The Runtime Failure#
When a checked construction needs a runtime check and the check fails, the enclosing function returns Error("invariant failed for 'T'") where T is the type name. It does not panic. This is the one place where a type invariant failure is a value rather than a panic.
type Positive = i32 : invariant self > 0
fn to_positive(n i32) Result[Positive, str] => Success(Positive { n }?)
fn main() {
match to_positive(5) {
Success(value) => print(value)
Error(reason) => print(reason)
}
match to_positive(0) {
Success(value) => print(value)
Error(reason) => print(reason) // invariant failed for 'Positive'
}
}
The caller can handle the failure locally, propagate it with ?, or convert it to a different error type. A checked construction is therefore an ordinary fallible operation, not a special panic path.
Record Construction#
A record with an invariant may need several field checks and a cross-field check. Checked construction performs all of them. Fields whose values already have the field's refinement type are not re-checked; fields whose values are raw values are checked against the field invariant; the record's own invariant is checked last.
type Positive = i32 : invariant self > 0
type Pair : invariant x == y && y > 10 {
x Positive
y Positive
}
fn build_pair(n i32) Result[Pair, str] {
const x = Positive { n }?
const y = Positive { n }?
return Success(Pair { x = x, y = y }?)
}
The first two constructions check n > 0. The record construction checks x == y and y > 10. If n is 5, the pair fails the cross-field invariant and the function returns an error. If n is 12, the pair succeeds.
Nested Invariants#
A field's refinement is checked when the field is constructed, and the record's invariant may refer to the refined field. If a field value is already refined, the record check can trust it. If a field value is raw, the record construction checks it as part of the whole.
type Positive = i32 : invariant self > 0
type Counter {
n Positive
}
fn bump(counter Counter) Result[Counter, str] {
counter.n = Positive { counter.n + 1 }?
return Success(counter)
}
Counter has no cross-field invariant, so the field may be assigned directly after the checked construction. Window in the previous chapter has a cross-field invariant, so it must be replaced as a whole.
Redundant Checks#
If the compiler can prove the construction, the ? is redundant. It produces a warning rather than an error, and the expression has type T.
const value = Positive { 5 }? // warning: redundant '?' check
The warning is useful during development: it shows that a check has become unnecessary, perhaps because a constant replaced a variable. Remove the ? when the value is genuinely known, or keep it if the expression may become runtime-dependent later. Do not silence the warning by changing the type.
? Is Overloaded#
The postfix ? has two meanings. After a record literal it means checked construction. After any other expression it means Result error propagation.
const value = Positive { n }? // checked construction
const payload = result? // Result propagation
The parser distinguishes the forms by the operand. A checked construction requires a record literal operand; a propagation requires a Result. The enclosing function requirements are different: checked construction requires Result[_, str] when a runtime check is needed, while propagation requires a Result with the same error type as the operand.
Restrictions#
Checked construction is not allowed inside a defer body, because ? is not allowed there. It is also not a way to construct a value in a non-Result function when a runtime check is required. Move the construction into a helper that returns Result, or prove the value at compile time.
A checked construction cannot be used to create a value of a type with no invariants in a non-Result function unless the construction is redundant. A plain record with no invariants does not need checked construction; use normal construction.
Release Mode#
-release removes the runtime invariant checks performed by checked construction. The expression then produces a value without verifying the invariant. This is unsafe unless the program has established the property by other means. Compile-time checks are still performed, so a constant-false construction is still rejected.
The idiomatic rule is to treat -release as a performance switch for programs that have been tested and reasoned about, not as a way to bypass a check that keeps failing.
Summary#
| Form | Requires Result[_, str] |
Failure |
|---|---|---|
T { ... } with constants |
No | Compile error |
T { ... } with runtime values |
Compile error | Must use ? |
T { ... }? with a runtime check |
Yes | Returns Error("invariant failed for 'T'") |
T { ... }? with a compile-time proof |
No | Warning; value is T |
T { ... }? with a known violation |
No | Compile error |
result? |
Yes, matching error type | Returns the original Error |