渲染zh-Hans

枚举

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

Declares an enumeration.

Aliases: enum

Syntax: 枚举 <name> <block>

Category#

type

Examples#

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

Declares an enumeration.

# =============================================================================
# 枚举 — Declares an enumeration.
# =============================================================================
#
# What this teaches:
#   • Enumeration declaration — `枚举 <Name> { member, ... }` declares a set of ordered, named constants.
#   • Discriminant values — Members can have explicit numeric values with `= <number>`.
#   • Pattern matching — `选择` with `情况` branches selects on 枚举 variants.
#
# Common mistakes:
#   • Confusing 枚举 (enum with named constants) with 判别 (tagged union with payloads) — 枚举 members carry no data.
#
# See also: 判别
# =============================================================================

# 枚举 — enumerated constants (ordered variants)
#
# 枚举 <Nomen> { <membrum>, … }
# 枚举 <Nomen> { <membrum> = <动态值>, … }
#
# GRAMMAR:
#   ordoDecl :← '枚举' ident '{' ordoMember (',' ordoMember)* '}'
#
# EXPECTED OUTPUT:
#   rubrum
#   actum

enum Color {
    rubrum,
    viridis,
    caeruleum
}

enum Condicio {
    pendens = 0,
    actum = 1,
    finitum = 2
}

main {
    # Using 枚举 values: members are qualified by their 枚举 name
    const Color color ← Color.rubrum
    const Condicio condicio ← Condicio.actum

    # Select by 枚举
    switch color {
        case Color.rubrum {
            print "rubrum"
        }
        case Color.viridis {
            print "viridis"
        }
        case Color.caeruleum {
            print "caeruleum"
        }
    }

    # Select by 枚举 with numeric values
    switch condicio {
        case Condicio.pendens {
            print "pendens"
        }
        case Condicio.actum {
            print "actum"
        }
        case Condicio.finitum {
            print "finitum"
        }
    }
}

Expected output:

rubrum
actum