Kết xuấtvi

hằng

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

Declares an immutable binding.

Aliases: const, immutable

Syntax: hằng <type|_> <pattern> [← <expression>]

Category#

binding

Examples#

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

Declares an immutable binding.

# =============================================================================
# hằng — Declares an immutable binding
# =============================================================================
#
# What this teaches:
#   • Declares an immutable binding.
#   • Related keywords: biến, đặt, ←
#
# Common mistakes:
#   • Reassigning a `hằng` binding after initialization — `hằng` is immutable; use `biến` for mutable bindings (SEM020) or defer init with a single later assignment.
#
# See also: biến, đặt, ←
# =============================================================================

# hằng — immutable bindings
#
# Immediate init:
#   hằng số count ← 0
#   hằng _ tên ← "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.
#   hằng số pending
#   pending ← 42
#
# `đặt x` is sugar for `hằng _ x` in both immediate and deferred shapes; see
# đặt/sit.fab for the compact inferred spelling.
#
# GRAMMAR:
#   varDecl := ('hằng' | 'biến') 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
}

test "fixum immediate init" {
    const string tên ← "Marcus"
    const string salve ← "Salve, §!"(tên)
    assert tên ≡ "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
}

main {
    const string tên ← "Marcus"
    const string salve ← "Salve, §!"(tên)
    print salve
    const int pending
    pending ← 7
    print pending
    print scale(true, 3)
    print scale(false, 3)
}

Expected output:

Salve, Marcus!
7
30
300