Variables & Constants

In SP, variables can be declared using type inference (let and var) or explicit type annotations.

variables.sp
1import std.io;
2
3fn main() -> void {
4 // 1. Immutable bindings with 'let' (recommended)
5 let language = "SP";
6 let version = 1.0;
7
8 // 2. Mutable bindings with 'var'
9 var counter = 0;
10 counter = counter + 1;
11
12 // 3. Explicit type annotations
13 int explicitAge = 25;
14 string explicitCity = "Stockholm";
15
16 println("${language} v${version} - Count: ${counter}");
17}

Immutability by Default with `let`

Variables declared with let are immutable once initialized. Reassigning a let variable triggers a compile-time error:

Compiler Error E1003
let x = 10; x = 20; // Error E1003: Cannot reassign immutable variable 'x'

Mutability with `var`

When a value needs to be reassigned, declare it with var. The type of the variable is inferred from the initial assignment and cannot change to an incompatible type.