渲染zh-Hans

fixum

Translation status: 简体中文 reader-locale proof. Code fences render through the zh-Hans pipeline; prose is canonical Latin.

Declares an immutable binding.

Aliases: const, immutable

Syntax: fixum <type|_> <pattern> [← <expression>]

Category#

binding

Examples#

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

Declares an immutable binding.

# =============================================================================
# fixum — Declares an immutable binding
# =============================================================================
#
# What this teaches:
#   • Declares an immutable binding.
#   • Related keywords: varia, sit, ←
#
# Common mistakes:
#   • Reassigning a `fixum` binding after initialization — `fixum` is immutable; use `varia` for mutable bindings (SEM020) or defer init with a single later assignment.
#
# See also: varia, sit, ←
# =============================================================================

# fixum — immutable bindings
#
# Immediate init:
#   fixum numerus count ← 0
#   fixum _ nomen ← "Marcus"
#
# Deferred init (write-once): declare without ←, assign exactly once later, then
# freeze. The definite-assignment pass rejects reads before that assignment and
# any second assignment.
#   fixum numerus pending
#   pending ← 42
#
# `sit x` is sugar for `fixum _ x` in both immediate and deferred shapes; see
# sit/sit.fab for the compact inferred spelling.
#
# GRAMMAR:
#   varDecl := ('fixum' | 'varia') typeAnnotation IDENTIFIER ('←' expression)?
#
# EXPECTED OUTPUT:
#   fixum.expected (Salve, Marcus! / 7 / 30 / 300)

fn scale(bool compact, int base)  int {
    const int factor
    if compact {
        factor  10
    }
    else {
        factor  100
    }
    return base * factor
}

main {
    const _ nomen  "Marcus"
    const _ salve  "Salve, §!"(nomen)

    print salve

    const int pending
    pending  7
    print pending

    print scale(true, 3)
    print scale(false, 3)
}

test "fixum immediate init" {
    const _ nomen  "Marcus"
    const _ salve  "Salve, §!"(nomen)
    assert nomen  "Marcus"
    assert salve  "Salve, Marcus!"
}

test "fixum deferred init writes once" {
    const int pending
    pending  7
    assert pending  7
}

test "fixum factor selected per branch" {
    assert scale(true, 3)  30
    assert scale(false, 3)  300
}

Expected output:

Salve, Marcus!
7
30
300