Skip to content

Variables and Constants

In Flame, variables are immutable by default to encourage safe, concurrent programming practices.


Use let to declare a variable. Once assigned, its value cannot be changed.

let language = "Flame"
let year = 2026
// Error: cannot mutate immutable variable
// year = 2027

Type annotations are optional when the compiler can infer the type:

let count: Int = 10
let rate: Float = 4.5
let message: String = "Hello"

To allow a variable to be reassigned, use let mut:

let mut counter = 0
counter = counter + 1
counter = counter * 2
print(counter) // Outputs: 2

Use const to declare a compile-time constant. Constants must always include an explicit type and can only be assigned expressions known at compile time:

const MAX_CONNECTIONS: Int = 1024
const APP_NAME: String = "FlameServer"
const TIMEOUT_SECONDS: Float = 30.0
Feature let const
Mutability Immutable by default (mut optional) Always strictly immutable
Type Annotation Optional (Inferred) Mandatory
Evaluation Time Runtime Compile-time
Scope Block-scoped Module- or Block-scoped

Flame supports powerful destructuring assignment, allowing you to extract multiple values from Formulas, JSON, Objects, or Tuples (Arrays) directly into distinct variables. This is particularly useful for returning multiple values from a function or handling channels.

// 1. Destructuring a Tuple (e.g. from thread.channel())
import std.thread
let (tx, rx) = thread.channel()
// 2. Destructuring an Object or Formula
let user = formula { name: "Alice", age: 30 }
let { name, age } = user
print($"Name: {name}, Age: {age}")
// 3. Destructuring JSON
import std.json
let parsed = json.parse("{\"status\": 200, \"data\": \"ok\"}")
let { status, data } = parsed
print($"Status: {status}")

Variables are lexically scoped to the { ... } block in which they are declared:

let x = 10
{
let y = 20
print(x + y) // 30
}
// print(y) // Error: `y` is not in this scope

Shadowing is also supported:

let data = "123"
let data: Int = data.toInt() // Shadows `data` with an Int type
print(data + 1) // 124