Variables
Declaration
Variables are declared with the var keyword. Type annotation is required.
var x:i32 = 42
var name:string = "stardyn"
var active:bool = true
var pi:number = 3.14
var b:u8 = 200
All available types and their semantics are documented in Type System.
Scope
StarLang uses block scope. Variables declared inside a {} block are only accessible within that block.
fn main():void {
var x:i32 = 10
if (x > 5) {
var y:i32 = 20
console.log(y) # OK
}
# console.log(y) # ERROR: y is not defined here
}
Unused Variables
The compiler enforces that every declared variable must be used. Declaring a variable without reading it is a compile error:
To intentionally discard a value, prefix the name with _. Any name starting with _ suppresses the check (Rust convention, not Go):
var _: number = 10.0 # OK — bare underscore
var _x: number = 10.0 # OK — prefixed name
var _temp: number = 10.0 # OK — still accessible if needed
Unlike Go's blank identifier (_), prefixed variables like _temp can still be read later.
The same rule applies to global variables and imports:
Assignment
Variables can be reassigned with =:
Type cannot be changed after declaration:
Collections
Array
Dict
var config:dict = {"host": "localhost", "port": 8080}
var ports:dict<string, i32> = {"http": 80, "https": 443}
Subscript Assignment
For generic type parameters, auto-coercion rules, and runtime type enforcement details, see Type System — Generic Types.