Skip to content

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.

Featureconstlet
ReassignmentProhibited at compile timeAllowed
Deep ImmutabilityEnforced recursively on objects and arraysNot enforced (properties and elements are mutable)
Initializer RequiredAlways requiredOptional (let x: i32; is allowed)
ShadowingProhibitedProhibited

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.

tyrus
const maxRetries: i32 = 3;
const appName: string = "tyrus";

print(maxRetries); // 3
print(appName); // tyrus

Attempting to assign a new value to a const binding produces a compile-time error.

error

tyrus
const maxRetries: i32 = 3;

maxRetries = 5; 
text
error[E0384]: cannot assign to immutable variable `maxRetries`
at /src/main.tyr:3:1
maxRetries = 5;
^^^^^^^^^^
help: declare `maxRetries` with `let` to allow assignment

Deep 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.

tyrus
const numbers: i32[] = [1, 2, 3];

print(numbers[0]); // 1
print(numbers.length); // 3

Mutating an object field or array element through a const reference is rejected by the compiler.

error

tyrus
const numbers: i32[] = [1, 2, 3];

numbers[0] = 99; 
text
error[E0384]: cannot assign through immutable `numbers`
at /src/main.tyr:3:1
numbers[0] = 99;
^^^^^^^
help: declare `numbers` with `let` to allow assignment

Mutable 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.

tyrus
let counter: i32 = 0;
counter = 1;
counter += 2;
print(counter); // 3

let scores: i32[] = [10, 20, 30];
scores[0] = 95;
print(scores[0]); // 95

Reassignment must still conform to the statically declared or inferred type of the variable.

error

tyrus
let counter: i32 = 0;

counter = "two"; 
text
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 match

Uninitialized 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.

tyrus
let total: i32;
total = 100;
print(total); // 100

let status: string?;
print(status ?? "ready"); // ready

Reading an uninitialized non-optional variable before assignment is an error, and declaring a const without an initializer is prohibited.

error

tyrus
let total: i32;

print(total); 
text
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 it

No 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.

tyrus
let count: i32 = 1;
count = 2;

print(count); // 2

Attempting to redeclare a variable with let or const in the same scope is rejected by the compiler.

error

tyrus
let count: i32 = 1;

let count: i32 = 2; 
text
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.

tyrus
const timeout: u32 = 5000;
let endpoint = "/api/v1";

print(timeout); // 5000
print(endpoint); // /api/v1

When an explicit type annotation is supplied, the assigned value must satisfy that type constraint.

error

tyrus
const timeout: u32 = "5000"; 
text
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>`