Language
Variables
Learn about mutable and immutable variables in Kronos
Variables
Kronos has two kinds of variables: immutable (set) and mutable (let). This distinction helps catch bugs early and makes code easier to reason about.
At a Glance
set name to "Alice"
set PI to 3.14159
# This would error:
# set name to "Bob" → Cannot reassign immutable variableUse set for values that shouldn't change — constants, configuration, computed values.
let counter to 0
let counter to 1 # OK
let counter to 2 # OK
let message to "Hello"
let message to "Goodbye" # OKUse let for values that need updating — counters, accumulators, loop state.
Immutable Variables
Immutable variables are declared with set and cannot be reassigned.
set x to 5
set name to "Alice"
set pi to 3.14159
set active to trueReassignment Error
set x to 10
set x to 20 # Error: Cannot reassign immutable variable 'x'Mutable Variables
Mutable variables are declared with let and can be freely reassigned.
let counter to 0
print counter # 0
let counter to counter plus 1
print counter # 1
let counter to 100
print counter # 100Type Annotations
Optionally specify a type using as. Once set, the variable can only hold values of that type.
let age to 25 as number
let age to 26 # OK (still a number)
let age to "twenty" # Error: Type mismatchAvailable Types
Prop
Type
Examples with Types
set name to "Bob" as string
let flag to true as boolean
let scores to list 95, 87, 92 as list
set config to map host: "localhost", port: 8080 as mapNaming Rules
| Rule | Valid | Invalid |
|---|---|---|
Start with letter or _ | name, _private | 123abc, -var |
Contains letters, numbers, _ | user_id, count2 | user-id, my.var |
| Case sensitive | myVar ≠ myvar | — |
| Not a reserved keyword | total, result | if, for, set |
Best Practices
When to use set vs let
Use set when:
- The value represents a constant (
set MAX_SIZE to 100) - The value is computed once and used later
- You want to prevent accidental reassignment
Use let when:
- The value needs to change (counters, accumulators)
- Building up a result iteratively
- Working with loop variables
Example: Using Both
# Constants that never change
set MAX_RETRIES to 3
set TIMEOUT_MS to 5000
# Mutable state that updates
let attempt to 0
let last_error to null
while attempt is less than MAX_RETRIES:
print f"Attempt {attempt plus 1} of {MAX_RETRIES}"
try:
# ... do something ...
break
catch error:
let last_error to error
let attempt to attempt plus 1
if last_error is not null:
print f"All attempts failed: {last_error}"