渲染zh-Hant

∪

Declares a type alias.

Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant pack; supporting prose may still be English.

Inline value unions and 比對 on 分支聯集 variants.

Syntax: 型別 <name> = <type> ∪ <type>

Category#

type

Examples#

radix/corpus/unio/unio.fab (canonical · operator-group)#

Inline value unions and 比對 on 分支聯集 variants.

# =============================================================================
# ∪ — Inline value unions and 比對 on 分支聯集 variants.
# =============================================================================
#
# What this teaches:
#   • Inline unions — `∪` creates ad-hoc union types without a full `分支聯集` declaration
#   • Tagged variants — `分支聯集` with `分支` pattern matching for structured payloads
#
# Common mistakes:
#   • confusing unio (ad-hoc union 型別 A ∪ B) with ∪ used as a set operator — unio is a type-level construct
#
# See also: 型別, 比對, 分支聯集
# =============================================================================




# Inline value unions and 比對 on 分支聯集
#
# 型別 Valor = 整數 ∪ 文字 models a non-nullable ad-hoc union.
# 分支聯集 + 比對 handles tagged variants with payloads.
#
# GRAMMAR:
#   typeAlias :← '型別' ident '=' typeExpr ('∪' typeExpr)*
#   比對  :← '比對' expr '{' '分支' variant pattern block ... '}'
#
# EXPECTED OUTPUT:
#   42, "salve", "n: 7", "t: faber"
#
# BACKEND: Inline-union `比對` enum lowering gap on Rust (whitelist: unio/unio.fab).

type Valor = int ∪ string

union Forma {
    Numerus { int v },
    Textus { string v }
}

fn describe(Forma f) → string {
    match f {
        case Numerus const v { return "n: §"(v) }
        case Textus const v { return "t: §"(v) }
    }
}

main {
    # --- Ad-hoc inline union values ---

    const Valor a ← 42
    const Valor b ← "salve"
    print a
    print b

    # --- Tagged 分支聯集 variants via 虛構 ---

    print describe(variant Numerus { v = 7 } ∷ Forma)
    print describe(variant Textus { v = "faber" } ∷ Forma)
}

Expected output:

42
salve
n: 7
t: faber