SPECTRALANG_

// DOCS NAVIGATION

Variáveis e Mutabilidade_

SOURCE: content/docs/reference/02-fundamentos.md02 Fundamentos / 2. Variáveis e Mutabilidade / Variables & Mutability

Declaração de Variáveis / Variable Declaration

PT-BR:
Variáveis são declaradas com a palavra-chave let. A anotação de tipo é opcional — o compilador infere o tipo a partir do valor inicial quando possível.

EN-US:
Variables are declared with the let keyword. The type annotation is optional — the compiler infers the type from the initial value when possible.

module variaveis

public func main() {
    // Com inferência de tipo / With type inference
    let x = 10
           // int
    let pi = 3.14
        // float
    let ativo = true
     // bool
    let nome = "Alice"
   // string
    let letra = 'A'
      // char

    // Com anotação de tipo explícita / With explicit type annotation
    let contador: int = 0
    let temperatura: float = 36.5
    let mensagem: string = "Olá"
    let flag: bool = false
    let caractere: char = 'Z'
}

Reatribuição / Reassignment

PT-BR:
Em SpectraLang, todas as variáveis são reatribuíveis após a declaração — não existe a distinção let/var ou const/let. A palavra-chave mut existe na gramática mas a mutabilidade é implícita para variáveis locais.

EN-US:
In SpectraLang, all variables are reassignable after declaration — there is no let/var or const/let distinction. The mut keyword exists in the grammar but mutability is implicit for local variables.

module mutabilidade

public func main() {
    let contador = 0
    contador = contador + 1
  // OK — reatribuição / reassignment
    contador = 10
            // OK

    let nome = "Alice"
    nome = "Bob"
             // OK

    // Também funciona com campos de array / Also works with array elements
    let arr = [1, 2, 3]
    arr[0] = 99
              // Modifica o primeiro elemento / Modifies first element
}

Escopo / Scope

PT-BR:
Variáveis existem no escopo do bloco { } em que foram declaradas. Variáveis em escopos internos podem sombrear variáveis externas.

EN-US:
Variables exist in the scope of the { } block in which they were declared. Variables in inner scopes can shadow outer variables.

module escopo

public func main() {
    let x = 10

    if x > 5 {
        let y = x * 2
    // 'y' só existe dentro do if / 'y' only exists inside the if
        let x = 999
      // sombra o 'x' externo / shadows outer 'x'
        // aqui x == 999 / here x == 999
    }
    // aqui x == 10, 'y' não existe / here x == 10, 'y' does not exist
}