const
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
Declares an immutable binding.
Aliases: const, immutable
Syntax: const <type|_> <pattern> [← <expression>]
Category#
binding
Related#
Examples#
radix/corpus/reference/keyword/fixum/fixum.fab (canonical · keyword)#
Declares an immutable binding.
# =============================================================================
# const — Declares an immutable binding
# =============================================================================
#
# What this teaches:
# • Declares an immutable binding.
# • Related keywords: mut, let, ←
#
# Common mistakes:
# • Reassigning a `let` binding after initialization — `let` is write-once; use `mut` for mutable bindings (SEM020) or defer init with a single later assignment.
#
# See also: mut, let, ←
# =============================================================================
# const — immutable bindings
#
# Immediate init:
# let int count ← 0
# let _ name ← "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.
# let int pending
# pending ← 42
#
# `let x` infers the type in both immediate and deferred shapes; see
# let/sit.fab for the compact inferred spelling. `const` itself now states a
# compile-time constant (`const T X = e`).
#
# GRAMMAR:
# varDecl := ('let' | 'mut') typeAnnotation IDENTIFIER ('←' expression)?
#
# EXPECTED OUTPUT:
# fixum.expected (Salve, Marcus! / 7 / 30 / 300)
fn scale(bool compact, int base) → int {
let int factor
if compact {
factor ← 10
}
else {
factor ← 100
}
return base * factor
}
test "fixum immediate init" {
let string name ← "Marcus"
let string salve ← "Salve, §!"(name)
assert name ≡ "Marcus"
assert salve ≡ "Salve, Marcus!"
}
test "fixum deferred init writes once" {
let int pending
pending ← 7
assert pending ≡ 7
}
test "fixum factor selected per branch" {
assert scale(true, 3) ≡ 30
assert scale(false, 3) ≡ 300
}
main {
let string name ← "Marcus"
let string salve ← "Salve, §!"(name)
print salve
let int pending
pending ← 7
print pending
print scale(true, 3)
print scale(false, 3)
}Expected output:
Salve, Marcus!
7
30
300