Kronos
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 variable

Use 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"  # OK

Use 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 true

Reassignment 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  # 100

Type 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 mismatch

Available 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 map

Naming Rules

RuleValidInvalid
Start with letter or _name, _private123abc, -var
Contains letters, numbers, _user_id, count2user-id, my.var
Case sensitivemyVarmyvar
Not a reserved keywordtotal, resultif, 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}"

On this page