Skip to content

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:

var x: number = 10.0   # ERROR: 'x' declared but not used

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:

import "math"           # ERROR: 'math' imported but not used

Assignment

Variables can be reassigned with =:

var x:i32 = 10
x = 20

Type cannot be changed after declaration:

var x:i32 = 10
x = "hello"               # ERROR: type mismatch

Collections

Array

var items:array = [1, "hello", true]
var nums:array<i32> = [10, 20, 30]

Dict

var config:dict = {"host": "localhost", "port": 8080}
var ports:dict<string, i32> = {"http": 80, "https": 443}

Subscript Assignment

nums[0] = 99
config["port"] = 9090

For generic type parameters, auto-coercion rules, and runtime type enforcement details, see Type System — Generic Types.