Kết xuấtvi

phân_tích

Translation status: Tiếng Việt reader-locale proof. Term names and code fences follow the vi pack; supporting prose may still be English.

Starts an exhaustive pattern match over a value or values.

Aliases: match

Syntax: phân_tích <subject> <block>

Category#

control-flow

Examples#

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

Starts an exhaustive pattern match over a value or values.

# =============================================================================
# phân_tích — Starts an exhaustive pattern match over a value or values.
# =============================================================================
#
# What this teaches:
#   • Exhaustive pattern matching — `phân_tích <value> { trường_hợp ... }` matches against all variants of a hợp_nhất (tagged union).
#   • Variant destructuring — `trường_hợp <Variant> hằng <field>, ...` destructures variant payload fields.
#   • Unit variants — variant without payload fields match by name alone.
#
# Common mistakes:
#   • Non-exhaustive match — omitting a variant from `phân_tích` without a `mặc_định` catchall; every hợp_nhất variant must be covered (SEM040).
#
# See also: hợp_nhất, trường_hợp
# =============================================================================

# phân_tích — pattern matching on hợp_nhất values
#
# phân_tích <value> {
#     trường_hợp <Casus> { <body> }
#     trường_hợp <Casus> như <alias> { <body> }
#     trường_hợp <Casus> hằng <bindings> { <body> }
#     trường_hợp <Casus> hằng <field> như <alias> { <body> }
# }
#
# GRAMMAR:
#   discerneStmt :← 'phân_tích' expr '{' casuClause* '}'
#
# EXPECTED OUTPUT:
#   phân_tích exempla parata

# hợp_nhất — tagged union / enum declaration
union Condicio {
    Agens,
    Iners,
    Pendens
}

union Nuntius {
    Pulsus {
        int x
        int y
    },
    Clavis {
        string clavis
    },
    Finis
}

# Simple trường_hợp 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"
        }
    }
}

# trường_hợp with hằng 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