Skip to content

Section 5: Variables, State, and Mutability

TECHSPEC-05 — Section 5 · Version 0.1 · Status: Initial Draft

Status: draft. This section is an Initial Draft (Section 57 is a Working Register). It describes intended or proposed behavior, not a finalized, implemented language rule. Do not treat examples here as normative.

1. Variable Philosophy

Variables must have explicit types and clearly defined lifetimes.

Values should be immutable by default. Modification must be explicitly declared.

2. Variable Declarations

The language supports:

  • LET: Declares an immutable variable.
  • VAR: Declares a mutable variable.
  • CONST: Declares a compile-time constant.

Examples:

LET accountId: Integer = 100

VAR balance: Decimal = 250.00

CONST MAX_ATTEMPTS: Integer = 3

Rules: - Every variable must have a declared type. - Variables must be initialized before use. - LET values cannot be reassigned. - VAR values may be reassigned within their permitted scope. - CONST values cannot change after compilation. - Implicit type conversions are prohibited.

3. Scope and Lifetime

Variables may exist within: - Module scope. - Function scope. - Block scope. - Object or struct scope.

Rules: - Variables must not be accessible outside their declared scope. - Inner scopes may access permitted outer-scope variables. - Inner scopes cannot silently modify immutable outer variables. - Variables cannot be accessed after their lifetime ends. - Shadowing existing variable names should be prohibited in the initial version.

4. State Management

State represents data that persists or changes during program execution.

Rules: - State must have a clearly defined owner. - State modifications must be explicitly declared. - Shared state requires controlled access. - State changes must preserve applicable invariants. - Functions must declare modifications to externally owned state. - State transitions must be compatible with the object's declared rules.

5. Mutation

Mutation is any operation that changes an existing value or state.

Rules: - Immutable values cannot be modified. - Mutable values can only be modified within their permitted ownership and access rules. - Functions cannot modify undeclared external state. - Mutations must preserve applicable type constraints and invariants. - Mutation through references or aliases must not bypass these restrictions.

6. Initialization

  • Variables must be initialized before they are read.
  • Struct fields must be initialized before an object is considered valid.
  • Partially initialized objects cannot be used as fully valid objects.
  • Initialization must establish all required invariants.
  • Invalid initialization must be rejected.

7. Compiler Responsibilities

The compiler must: - Track variable declarations and scopes. - Detect uninitialized reads. - Reject reassignment of immutable values. - Validate mutation permissions. - Track ownership and lifetimes. - Detect prohibited aliasing and state modifications. - Check that state changes preserve applicable constraints.

8. Design Goal

AI agents must have a clear understanding of which values they can change, which values they must preserve, and which state modifications are permitted.

Unintended mutation must be prevented wherever the language can enforce it.


Source file: TECHSPEC-5.md (preserved in source-original/LANG/). See the specification index, the glossary, and the cross-reference index.