匹配
Translation status: 简体中文 reader-locale proof. Term names and code fences follow the zh-Hans 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