Variables and Constants
In Flame, variables are immutable by default to encourage safe, concurrent programming practices.
Immutable Variables (let)
Section titled “Immutable Variables (let)”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 = 2027Type annotations are optional when the compiler can infer the type:
let count: Int = 10let rate: Float = 4.5let message: String = "Hello"Mutable Variables (let mut)
Section titled “Mutable Variables (let mut)”To allow a variable to be reassigned, use let mut:
let mut counter = 0counter = counter + 1counter = counter * 2
print(counter) // Outputs: 2Compile-Time Constants (const)
Section titled “Compile-Time Constants (const)”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 = 1024const APP_NAME: String = "FlameServer"const TIMEOUT_SECONDS: Float = 30.0Differences Between let and const
Section titled “Differences Between let and const”| 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 |
Destructuring Assignment
Section titled “Destructuring Assignment”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.threadlet (tx, rx) = thread.channel()
// 2. Destructuring an Object or Formulalet user = formula { name: "Alice", age: 30 }let { name, age } = userprint($"Name: {name}, Age: {age}")
// 3. Destructuring JSONimport std.jsonlet parsed = json.parse("{\"status\": 200, \"data\": \"ok\"}")let { status, data } = parsedprint($"Status: {status}")Variable Scoping & Shadowing
Section titled “Variable Scoping & Shadowing”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 scopeShadowing is also supported:
let data = "123"let data: Int = data.toInt() // Shadows `data` with an Int typeprint(data + 1) // 124