Contents

Normal construction T { ... } has type T and must be provably valid. 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 }?)

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 error type must be exactly str, because the runtime failure message is a string.

Runtime Failure#

A failed checked construction returns Error("invariant failed for 'T'"); it does not abort. This is the only place where a type invariant failure is a value.

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 may handle the failure locally, propagate it with ?, or convert it to a different error type.

Record Construction#

Checked construction checks each field and then the record's own invariant. A field whose value already has the field's refinement type is not re-checked.

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 field constructions check n > 0. The record construction checks x == y and y > 10.

Nested Invariants#

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 after the checked construction. A record with a cross-field invariant must be replaced as a whole.

Redundant Checks#

If the compiler can prove the construction, ? produces a warning and the expression has type T.

const value = Positive { 5 }?      // warning: redundant '?' check

? Is Overloaded#

After a record literal, ? means checked construction. After any other expression, it means Result propagation.

const value = Positive { n }?      // checked construction
const payload = result?            // Result propagation

Checked construction requires Result[_, str] when a runtime check is needed. 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 cannot 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 plain record with no invariants does not need checked construction.

Release Mode#

-release removes the runtime invariant checks performed by checked construction. The expression then produces a value without verifying the invariant. Compile-time checks remain, so a constant-false construction is still rejected.

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