Chapter 5. Variables and Constants

5.1 General

Variables and constants associate names with values.

A variable denotes a storage location whose value may change during program execution.

A constant denotes a value that, once established, cannot subsequently be modified.

Both variables and constants are introduced by declarations.


5.2 Variable Declarations

A variable declaration introduces one or more variables into the current scope.

A declaration specifies:

  • the variable name
  • the variable type, either explicitly or through type inference
  • an optional initializer
  • any modifiers permitted by the language

The variable becomes visible immediately after its declaration, subject to the scope rules described in Chapter 3.


5.3 Variable Lifetime

The lifetime of a variable is determined by the declaration that introduces it.

Local variables are created when execution enters their enclosing scope and cease to exist when execution leaves that scope.

Member variables exist for the lifetime of the object or type to which they belong.

Static storage duration, if supported, is described in the chapter covering declarations.


5.4 Initialization

A variable may be initialized as part of its declaration.

When an initializer is present, the initializer expression is evaluated before the variable becomes available for subsequent use.

When no initializer is supplied, the compiler provides the default value for the variable’s type, unless the language requires definite assignment before use.


5.5 Type Inference

Where permitted, the compiler may infer the type of a variable from its initializer.

For example, if an initializer produces a value of type Integer, the declared variable acquires that type without requiring it to be written explicitly.

Type inference affects only the declaration syntax.

The inferred type is fixed once compilation succeeds.


5.6 Assignment

Assignment replaces the current value stored by a variable.

The value being assigned shall be compatible with the variable’s declared type.

Assignment does not alter the variable’s type.

Where a conversion is required, the conversion rules defined in Chapter 4 apply.


5.7 Definite Assignment

A variable shall not be read before a value has been assigned to it.

The compiler performs flow analysis to determine whether every execution path initializes the variable before its first use.

Failure to satisfy the definite assignment rules results in a compile-time error.


5.8 Constants

A constant declaration introduces a named value that cannot be modified after its initialization.

Every constant shall be initialized exactly once.

The initializer shall satisfy the restrictions imposed by the language for constant expressions.

Attempting to assign a new value to a constant is a compile-time error.


5.9 Scope

Variables and constants obey the lexical scope rules defined in Chapter 3.

A declaration hides any declaration of the same name in an enclosing scope.

Programs should avoid unnecessary shadowing, as it reduces readability.


5.10 Capture

When a local variable is referenced by a nested function, delegate, or anonymous routine, the implementation may capture the variable rather than its current value.

The semantics of captured variables are described in the chapter covering delegates and closures.


5.11 Summary

Variables provide mutable storage during program execution.

Constants provide immutable named values.

Together they form the foundation upon which expressions, statements, and callable routines operate.