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

constlet
EvaluatedAt compile timeAt run time
Type annotationRequiredOptional (inferred)
Module scopeAllowed
InitializerConstant expressionAny 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

TopicDescription
Intrinsic ConstantsCompiler-provided values such as #source.line

See Also