Skip to content

2.3. Variables, Constants, References, and Scope

A variable names storage whose value can change. A constant names a value known during compilation. A reference gives another name to existing storage. A scope controls where a declaration can be found.

use print

const LIMIT : int = 10

func *Main():
    var count : int = 2
    ref alias = count
    alias += 3
    PrintLn("{} / {}", [count, LIMIT]) // 5 / 10
endfunc

2.3.1 Variables and Initialization

Declare a variable with var name : Type, normally followed by = initializer. An assignment replaces its current value:

var count : int = 0
count = 3
count += 1

Initialize scalar locals before reading them. A declaration such as var count : int reserves storage but does not make its value readable; the compiler tracks definite assignment across branches and reports uninitialized uses. A function result must likewise be assigned on every normal exit path.

Sized embedded strings are initialized automatically: var s : embstr(32) starts empty and can be read or appended to immediately. See Embedded Strings.

Global storage is zero/default-initialized before program startup. Types with managed lifetimes, including dynamic arrays and autofree references, have their own empty/default initialization and cleanup rules. Use an explicit initializer when the intended value matters. {} explicitly zero/default-initializes a supported aggregate such as a structure or fixed array.

Object fixed storage uses <- rather than =; see Objects and Properties.

2.3.2 Restricted Type Inference

Write ? in place of a declared type when an initializer independently supplies an eligible concrete typed pointer, structure, or object-reference type:

struct SPoint:
    x : int
    y : int
endstruct

var point : SPoint = {10, 20}
var copy : ? = point
var address : ? = %point

Inference requires = and preserves the initializer's static type. Numeric, Boolean, text, enum, raw pointer, and other unlisted types are not inferred. In particular, var n : ? = 3 and var p : ? = nil are errors. The separate array notation [?]T infers a fixed array's length, not its element type.

For local variables initialized by new or TryCast(...), the type annotation can be omitted entirely. For example, var child = TryCast(OChild, base) infers OChild from the first argument and creates an ordinary reference without autofree. See Checked Object Casts.

new autofree T provides a shorthand for declaring an autofree variable; ordinary ? inference does not add autofree. See Memory Management and Autofree.

2.3.3 Local References

ref name = existing_value binds an alias to addressable compatible storage. Writes through the alias update the original storage. The binding must remain valid throughout the alias's use. A reference is not a nullable pointer value.

A reference cannot extend the lifetime of a local, a temporary, an array element, or a property result. Function reference parameter modes are described in Functions.

2.3.4 Constants

const requires a compile-time constant initializer:

const BUFFER_SIZE : int = 4096
const HALF_BUFFER : int = BUFFER_SIZE div 2

Related constants can share a declared type:

const(uint32):
    FLASH_BASE = 0x08000000
    FLASH_END  = FLASH_BASE + 0x7FFF
    SRAM_BASE  = 0x20000000
endconst

The group does not create another scope. Later constants can use earlier ones. Restricted ? inference follows the variable rules and does not remove the constant-expression requirement; a grouped const(Type) header cannot use ?.

FirstInt, FirstUint, FirstFloat, and FirstBool provide optional constant fallbacks; see Compiler Directives.

2.3.5 Parameterized Expressions with constexpr

A constexpr names a single expression with typed parameters and an explicit result type. Call it with parentheses, even when it takes no arguments. Only value parameters are supported. Trailing parameters may have constant defaults. Reference parameter modes and variable argument lists are invalid.

constexpr EntryCount(channels : int, entriesPerChannel : int = 2) : int =
    channels * entriesPerChannel

const SCOPE_ENTRIES : int = EntryCount(4)

You can also write -> ResultType in place of : ResultType.

The compiler checks the body against the result type and substitutes each argument into the body when compiling a call. In a constant context, the expanded expression must evaluate to a constant. Calls can also use runtime arguments in executable code. Because a parameter may appear more than once or be omitted from the body, pass side-effecting expressions through a local variable first when you need them to run exactly once.

The body can use supported operators, casts, aggregate literals, built-ins such as iif and Round, and calls to previously declared constexpr expressions. Calls to ordinary functions and captures of enclosing runtime locals are rejected in the body. Pass a local value as an argument instead.

The result type supplies the context for a structure initializer, so the same expression can build table entries that carry immediate values or addresses.

struct SParamDef:
    flags : uint32
    var_ptr : pointer
    reserved : pointer
endstruct

const PAR_INT32_CONST : uint32 = 0x300
const PAR_INT32_RO : uint32 = 0x100
var g_counter : int32 = 0

constexpr PTENTRY_VAR(flags : uint32, v : pointer) : SParamDef = { flags, v, ? }
constexpr PTENTRY_CONST(n : int32) : SParamDef =
    PTENTRY_VAR(PAR_INT32_CONST, pointer(int(n)))

const PARAMS : [?]SParamDef = [
    PTENTRY_CONST(7),
    PTENTRY_VAR(PAR_INT32_RO, %g_counter),
    {}
]

Here the first entry carries the integer in the pointer field. The second entry carries the address of global storage, which the linker resolves.

Declare a constexpr at module scope or inside a function. Module declarations follow the usual visibility rules. Public declarations can be imported, aliased, selected with only, or reexported. Names in the body resolve where the expression is declared. See Modules and Packages.

2.3.6 Lexical Scope and Declaration Order

Lookup starts in the innermost active scope and proceeds outward. A declaration must be visible before a use that needs it. Forward declarations explicitly reserve function or object names when their implementations come later. Declarations in an inner block are not visible after that block finishes.

Shadowing a visible name can produce a warning. Choosing distinct names makes it easier to tell which value an expression reads. A local reference and an ordinary variable follow the same scope visibility rules.

2.3.7 Module and Object Visibility

Module declarations before implementation are public. Declarations after it are private unless they implement an earlier public declaration. Imported symbols participate in unqualified lookup according to the use modifiers.

@.Name explicitly selects the current module's global scope. @module.Name selects an imported module namespace. Object methods prioritize members and require qualification for outside value symbols unless an allowed method-local use makes them available. Type names remain available for type annotations and casts.

Object members have public, protected, and private visibility; their rules are in Objects and Properties. See Modules and Packages for import aliases, merged symbols, and reexports.