Variables
Tyrus provides two declaration keywords for bindings: const and let. Both enforce strict lexical scoping and participate in static type checking, but they differ in mutability, rebinding rules, and initialization requirements.
const vs let
The choice between const and let establishes whether a binding and its underlying data can be mutated.
| Feature | const | let |
|---|---|---|
| Reassignment | Prohibited at compile time | Allowed |
| Deep Immutability | Enforced recursively on objects and arrays | Not enforced (properties and elements are mutable) |
| Initializer Required | Always required | Optional (let x: i32; is allowed) |
| Shadowing | Prohibited | Prohibited |
Immutable Bindings with const
const declares an immutable binding whose value cannot be reassigned after initialization. Use const by default whenever a variable does not require reassignment.
const maxRetries: i32 = 3;
const appName: string = "tyrus";
print(maxRetries); // 3
print(appName); // tyrusAttempting to assign a new value to a const binding produces a compile-time error.
error
const maxRetries: i32 = 3;
maxRetries = 5; error[E0384]: cannot assign to immutable variable `maxRetries`
at /src/main.tyr:3:1
maxRetries = 5;
^^^^^^^^^^
help: declare `maxRetries` with `let` to allow assignmentDeep Immutability with const
In Tyrus, const guarantees deep immutability: it prevents rebinding and prohibits modifying the properties of an object or the elements of an array declared with const.
const numbers: i32[] = [1, 2, 3];
print(numbers[0]); // 1
print(numbers.length); // 3Mutating an object field or array element through a const reference is rejected by the compiler.
error
const numbers: i32[] = [1, 2, 3];
numbers[0] = 99; error[E0384]: cannot assign through immutable `numbers`
at /src/main.tyr:3:1
numbers[0] = 99;
^^^^^^^
help: declare `numbers` with `let` to allow assignmentMutable Bindings with let
let declares a mutable binding. It allows both variable reassignment and in-place mutation of composite structures such as arrays and objects.
let counter: i32 = 0;
counter = 1;
counter += 2;
print(counter); // 3
let scores: i32[] = [10, 20, 30];
scores[0] = 95;
print(scores[0]); // 95Reassignment must still conform to the statically declared or inferred type of the variable.
error
let counter: i32 = 0;
counter = "two"; error[E0308]: type mismatch for `counter`: expected `i32`, found `string`
at /src/main.tyr:3:11
counter = "two";
^^^^^
help: change the value or the annotation so their types matchUninitialized let Bindings
A variable declared with let can be declared without an immediate initializer, even for non-optional types such as let number: i32;. Optional bindings (T?) start empty and can be evaluated safely with null-coalescing (??), while non-optional bindings must be assigned before they can be read.
let total: i32;
total = 100;
print(total); // 100
let status: string?;
print(status ?? "ready"); // readyReading an uninitialized non-optional variable before assignment is an error, and declaring a const without an initializer is prohibited.
error
let total: i32;
print(total); error[E0381]: `total` is used before being assigned a value
at /src/main.tyr:3:7
print(total);
^^^^^
help: assign a value to `total` before reading itNo Shadowing Allowed
Tyrus does not permit variable shadowing within the same scope. To update a value, declare a mutable binding with let and reassign it instead of redeclaring the variable.
let count: i32 = 1;
count = 2;
print(count); // 2Attempting to redeclare a variable with let or const in the same scope is rejected by the compiler.
error
let count: i32 = 1;
let count: i32 = 2; error[E0428]: identifier `count` is declared more than once in this scope
at /src/main.tyr:3:5
let count: i32 = 2;
^^^^^
related: previous declaration of `count` is here
at /src/main.tyr:1:5
let count: i32 = 1;
^^^^^Type Inference and Annotations
Variable types can be explicitly annotated or inferred automatically from the initializer expression.
const timeout: u32 = 5000;
let endpoint = "/api/v1";
print(timeout); // 5000
print(endpoint); // /api/v1When an explicit type annotation is supplied, the assigned value must satisfy that type constraint.
error
const timeout: u32 = "5000"; error[E0308]: type mismatch for `timeout`: expected `u32`, found `string`
at /src/main.tyr:1:7
const timeout: u32 = "5000";
^^^^^^^
help: change the value or the annotation so their types match; convert numbers explicitly with `value as <type>`