渲染zh-Hant

custodi

Translation status: 繁體中文 reader-locale proof. Code fences render through the zh-Hant pipeline; prose is canonical Latin.

Groups early-exit guard checks before the main body of a function.

Aliases: guard

Syntax: custodi <block>

Category#

control-flow

Examples#

radix/corpus/custodi/custodi.fab (canonical · keyword)#

Groups early-exit guard checks before the main body of a function.

# =============================================================================
# custodi — Groups early-exit guard checks before the main body of a function.
# =============================================================================
#
# What this teaches:
#   • Guard blocks — `custodi { si <conditio> { <exitus> } }` groups early-exit checks before a function's main logic.
#   • Separation of concerns — guard blocks keep preconditions clearly separated from the main body.
#   • Multiple guards — multiple `si` conditions in one custodi block, each with its own exit path.
#
# Common mistakes:
#   • Using `custodi` outside a function — guard blocks are only valid inside functio bodies.
#
# See also: si, redde
# =============================================================================

# custodi — guard blocks for early exit
#
# custodi { si <conditio> { <exitus> } }
#
# GRAMMAR:
#   custodiStmt :← 'custodi' '{' stmt* '}'
#
# EXPECTED OUTPUT:
#   Division, clamping, and range-validation scalar results.

fn divide(int a, int b)  int {
    guard {
        if b  0 {
            return 0
        }
    }

    return a / b
}

fn tracta(int x)  int {
    guard {
        if x < 0 {
            return -1
        }
        if x > 100 {
            return -1
        }
    }

    # Main logic, clearly separated from custodi
    return x * 2
}

fn stringe(int value, int minimum, int maximum)  int {
    guard {
        if minimum > value {
            return minimum
        }
        if maximum < value {
            return maximum
        }
    }

    return value
}

main {
    # Guard returns 0 instead of dividing by zero
    print divide(10, 2)
    print divide(10, 0)

    # Out-of-range inputs short-circuit to -1
    print tracta(50)
    print tracta(-10)
    print tracta(150)

    print stringe(5, 0, 10)
    print stringe(-5, 0, 10)
    print stringe(15, 0, 10)
}

Expected output:

5
0
100
-1
-1
5
0
10