Nyx by Example

Variables

Nyx distinguishes between immutable bindings (let) and mutable variables (var). Types are written explicitly after a colon, and string interpolation with ${...} makes it easy to embed values inside strings.

Code

// Variables: let (inmutable), var (mutable), tipos básicos e interpolación

fn main() -> int {
    // let es inmutable
    let nombre: String = "Nyx"
    let version: int = 12
    let activo: bool = true
    let precio: float = 3.14

    print("Nombre: ${nombre}")
    print("Version: ${version}")
    print("Activo: ${activo}")

    // var es mutable — se puede reasignar
    var contador: int = 0
    contador = contador + 1
    contador = contador + 1
    print("Contador: ${contador}")

    // Interpolación con expresión
    let doble: int = contador * 2
    print("Doble: ${doble}")

    // Concatenación de strings
    let saludo: String = "Hola, " + nombre + "!"
    print(saludo)

    return 0
}

Output

Nombre: Nyx
Version: 12
Activo: true
Contador: 2
Doble: 4
Hola, Nyx!

Explanation

let declares an immutable binding. Once assigned, you cannot reassign it — any attempt to do so is a compile-time error. This is the default and preferred form in Nyx, since most values do not need to change.

var declares a mutable variable. You can reassign it freely after its initial declaration. Use var only when mutation is genuinely needed — counters, accumulators, and loop variables are good candidates.

The four basic scalar types shown here are String (UTF-8 text), int (64-bit signed integer), bool (true or false), and float (64-bit double-precision floating point).

String interpolation uses the ${expr} syntax inside a double-quoted string. Any expression can appear inside the braces, and its value is automatically converted to a string. You can also concatenate strings with the + operator.

← Previous Next →

Source: examples/by-example/02-variables.nx