แยก
Translation status: ภาษาไทย reader-locale proof. Term names and code fences follow the th-TH 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