Variables and Constants
DLiteScript supports both mutable variables and immutable constants, both of which require explicit type declarations.
Variable Declaration
Variables are declared using the var keyword followed by a name, type, and optional initial value.
Syntax
| |
Examples
| |
Reassignment
Variables can be reassigned after declaration:
| |
Constant Declaration
Constants are declared using the const keyword.
Once initialized, their values cannot be changed.
Syntax
| |
Constants must be initialized at declaration. You cannot declare a constant without a value.
Examples
| |
Immutability
Attempting to reassign a constant results in an error:
| |
Scoping
Both variables and constants follow block scoping rules. A variable or constant declared within a block is only accessible within that block and any nested blocks.
Global Scope
Declarations at the top level are accessible throughout the entire file:
| |
Block Scope
Declarations inside blocks are only accessible within that block:
| |
Nested Scopes
Inner blocks can access variables from outer blocks, but not vice versa:
| |
Shadowing
Variables and constants in inner scopes can shadow (hide) names from outer scopes:
| |
Type Annotations
All variable and constant declarations must include explicit type annotations. DLiteScript does not perform type inference.
| |
Naming Conventions
Variable and constant names:
- Must start with a letter or underscore
- Can contain letters, numbers, and underscores
- Are case-sensitive
- Cannot be reserved keywords
| |