Constants
A constant binds a name to a value evaluated at compile time. Constants are declared with const, are always immutable, and must have an explicit type annotation. Unlike let bindings, they may be declared at module scope, which makes them the natural home for shared limits and mathematical values.
const MaxConnections: uint32 = 100;
const Pi: float64 = 3.14159265358979;
By convention, constant names use PascalCase (see Identifiers).
Constants vs. let Bindings
const | let | |
|---|---|---|
| Evaluated | At compile time | At run time |
| Type annotation | Required | Optional (inferred) |
| Module scope | Allowed | — |
| Initializer | Constant expression | Any expression |
The initializer must be a constant expression — a value the compiler can compute without running the program. It may combine literals and other constants:
const Width: uint32 = 1920;
const Height: uint32 = 1080;
const PixelCount: uint32 = Width * Height; // computed at compile time
Example
const MaxRetries: int = 5;
func Fetch() -> bool {
var attempts = 0;
while attempts < MaxRetries {
if TryFetch() { return true; }
attempts += 1;
}
return false;
}
Further Topics
| Topic | Description |
|---|---|
| Intrinsic Constants | Compiler-provided values such as #source.line |
See Also
- Immutable Variables (
let) — run-time immutable bindings - Intrinsic Constants — compile-time
#source.file,#source.line, and friends - Identifiers — naming conventions for constants
Mutability of Structs
For a simple value like an int, the choice between let and var only decides whether the variable can be reassigned. For a struct it decides more: because a struct is a value type, the binding that holds it governs every field. Mutability is deep — there is no way to have an immutable struct with mutable fields, or the reverse.
Intrinsic Constants
Alongside the constants you declare, the compiler exposes a set of intrinsic values through the build context. Each is a field of an intrinsic — #source, #build, #target, #compiler, or #config — that you import from the standard Rux package and read with .. Because every one is fixed at compile time, it can initialize an ordinary const or serve as a default argument, at no run-time cost.