Variables and Assignment
Demonstrates let bindings, reassignment, and string interpolation.
Example 002 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.
How it is built
The cell Variables has no face and no memory. Handlers: run (line 15).
The source
002_variables.cell
// ============================================================================
// 002 — Variables and Assignment
// ============================================================================
// Demonstrates let bindings, reassignment, and string interpolation.
//
// Run: soma run 002_variables.cell run
// Expect:
// name = Alice
// age = 30
// age after birthday = 31
// greeting = Hello, Alice! You are 31 years old.
cell Variables {
on run() {
// "let" creates a new variable binding
let name = "Alice"
let age = 30
// String interpolation uses {variable} inside double-quoted strings
print("name = {name}")
print("age = {age}")
// Variables can be reassigned (no "mut" keyword needed)
age = age + 1
print("age after birthday = {age}")
// You can interpolate multiple variables and field accesses
let greeting = "Hello, {name}! You are {age} years old."
print("greeting = {greeting}")
return greeting
}
}What the toolchain says
$ soma check 002_variables.cell
✓ All checks passed.$ soma run 002_variables.cell run
name = Alice
age = 30
age after birthday = 31
greeting = Hello, Alice! You are 31 years old.
Hello, Alice! You are 31 years old.