守衛
Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant pack; supporting prose may still be English.
Groups early-exit guard checks before the main body of a function.
Aliases: guard
Syntax: 守衛 <block>
Category#
control-flow
Related#
Examples#
radix/corpus/custodi/custodi.fab (canonical · keyword)#
Groups early-exit guard checks before the main body of a function.
# =============================================================================
# 守衛 — Groups early-exit guard checks before the main body of a function.
# =============================================================================
#
# What this teaches:
# • Guard blocks — `守衛 { 若 <conditio> { <出口> } }` 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 `若` conditions in one 守衛 block, each with its own exit path.
#
# Common mistakes:
# • Using `守衛` outside a function — guard blocks are only valid inside 函式 bodies.
#
# See also: 若, 傳回
# =============================================================================
# 守衛 — guard blocks for early exit
#
# 守衛 { 若 <conditio> { <出口> } }
#
# GRAMMAR:
# custodiStmt :← '守衛' '{' 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 守衛
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