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 # 100Unpacking Assignment
Multiple variables can be assigned at once using unpacking syntax. This works with both set (immutable) and let (mutable).
Multiple Assignment
set x, y to 10, 20
print x # 10
print y # 20
let a, b, c to 1, 2, 3
print a # 1
print b # 2
print c # 3Variable Swapping
Unpacking enables elegant variable swapping without a temporary variable:
let x to 10
let y to 20
print x # 10
print y # 20
let x, y to y, x
print x # 20
print y # 10Unpacking Function Returns
Functions can return multiple values, which can be unpacked into variables:
function get_coordinates:
return 100, 200
set x, y to call get_coordinates
print x # 100
print y # 200The number of variables must match the number of values being unpacked.
set x, y to 1, 2, 3 # Error: count mismatchType 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 mapGeneric Type Annotations
let scores to list 95, 87, 92 as list<number>
let grades to map "alice": 95, "bob": 88 as map<string, number>Union Type Annotations
let result to 42 as number or string
let result to "forty-two" # Also validType Aliases
type Point to map x: number, y: number
set origin to map x: 0, y: 0 as PointNaming 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}"