Renderingen-US

Experimental through version 1. Version 0 was alpha. This is the first language release: the interface is relatively stable, not frozen. Nothing is promised stable until version 2.

require

Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.

Require a condition or throw a recoverable error.

Aliases: require

Syntax: require <expression> throw <expression>

Category#

error

Examples#

radix/corpus/reference/keyword/requirit/requirit.fab (canonical · keyword)#

Require a condition or throw a recoverable error.

# =============================================================================
# require — Require a condition or throw a recoverable error.
# =============================================================================
#
# What this teaches:
#   • Recoverable guard — `require <condition> throw <error>` throws into the
#     function's ⇥ channel when the condition is false (en surface of `require`)
#   • Twin of `assert` — `assert` is fatal; `require` is catchable by `catch`
#   • Typed payload — the false-path expression is a `variant` with `causa`,
#     the same shape used at constructor guard ladders
#
# Common mistakes:
#   • Using `require` in a function without `⇥ E` — SEM010 `iace_requires_alternate_exit`, same as `throw`
#   • Treating `require` as a test-capability gate — it is a statement, not a named fixture on `test`
#
# See also: assert, throw, catch, ⇥
# =============================================================================

# require — recoverable require statement
#
# require <condition> throw <error>
#
# GRAMMAR:
#   requiritStmt :← 'require' expr 'throw' expr
#
# EXPECTED OUTPUT:
#   empty — success-path main only (requirit.expected)

union GuardError {
    Invalid {
        string causa
    }
}

fn guarded(int value) → int ⇥ GuardError {
    require value ≻ 0 throw variant Invalid { causa = "value must be positive" }
    return value
}

test "false path is recoverable" {
    do {
        let int dropped ← guarded(0)
        assert false panic "false path must throw"
    }
    catch err {
        # recovered — require entered ⇥ and catch intercepted it
    }
}

main {
    do {
        let int ok ← guarded(1)
        assert ok ≡ 1
    }
    catch err {
        assert false panic "success path must not throw"
    }
}

radix/corpus/reference/reject/requirit/requirit-iace-reject.fab (supporting · reject)#

Require always names its payload with throw; the missing particle is refused by name.

# =============================================================================
# require — a require guard must name its payload
# =============================================================================
#
# What this teaches:
#   • `require cond throw err` always carries the `throw` particle (en `throw`)
#     and the error value to throw. The particle is mandatory, so a guard that
#     drops it is refused by name (PARSE001 requirit_iace).
#   • The statement desugars to `if not (cond) { throw err }`: there is no
#     recoverable failure without something to throw.
#
# Common mistakes:
#   • writing the guard as a bare condition — add `throw <error>` after it.
# =============================================================================

main {
    require 1 ≡ 1
}

Expected: compilation rejects this example.