渲染zh-Hans

∪

Declares a type alias.

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