比對
Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant pack; supporting prose may still be English.
Starts an exhaustive pattern match over a value or values.
Aliases: match
Syntax: 比對 <subject> <block>
Category#
control-flow
Related#
Examples#
radix/corpus/discerne/discerne.fab (canonical · keyword)#
Starts an exhaustive pattern match over a value or values.
# =============================================================================
# 比對 — Starts an exhaustive pattern match over a value or values.
# =============================================================================
#
# What this teaches:
# • Exhaustive pattern matching — `比對 <value> { 分支 ... }` matches against all variants of a 分支聯集 (tagged union).
# • Variant destructuring — `分支 <Variant> 定值 <field>, ...` destructures variant payload fields.
# • Unit variants — variant without payload fields match by name alone.
#
# Common mistakes:
# • Non-exhaustive match — omitting a variant from `比對` without a `預設` catchall; every 分支聯集 variant must be covered (SEM040).
#
# See also: 分支聯集, 分支
# =============================================================================
# 比對 — pattern matching on 分支聯集 values
#
# 比對 <value> {
# 分支 <Casus> { <body> }
# 分支 <Casus> 作為 <alias> { <body> }
# 分支 <Casus> 定值 <bindings> { <body> }
# 分支 <Casus> 定值 <field> 作為 <alias> { <body> }
# }
#
# GRAMMAR:
# discerneStmt :← '比對' expr '{' casuClause* '}'
#
# EXPECTED OUTPUT:
# 比對 exempla parata
# 分支聯集 — tagged union / enum declaration
union Condicio {
Agens,
Iners,
Pendens
}
union Nuntius {
Pulsus {
int x
int y
},
Clavis {
string clavis
},
Finis
}
# Simple 分支 matching on unit variants (no payload)
fn nomina(Condicio s) → string {
match s {
case Agens {
return "agens"
}
case Iners {
return "iners"
}
case Pendens {
return "pendens"
}
}
}
# 分支 with 定值 bindings destructures variant fields
fn tracta(Nuntius e) → none {
match e {
case Pulsus const x, y {
print "pulsus: § §"(x, y)
}
case Clavis const clavis {
print "clavis: §"(clavis)
}
case Finis {
print "finis"
}
}
}
main {
# nomina/tracta are defined for downstream exempla; smoke check only
print "discerne exempla parata"
}Expected output:
discerne exempla parata