Variables & Constants
In SP, variables can be declared using type inference (let and var) or explicit type annotations.
variables.sp
1import std.io;23fn main() -> void {4 // 1. Immutable bindings with 'let' (recommended)5 let language = "SP";6 let version = 1.0;78 // 2. Mutable bindings with 'var'9 var counter = 0;10 counter = counter + 1;1112 // 3. Explicit type annotations13 int explicitAge = 25;14 string explicitCity = "Stockholm";1516 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.