SPECTRALANG_

// DOCS NAVIGATION

Intervalos_

SOURCE: content/docs/reference/03-tipos-compostos.md03 Tipos Compostos / 3. Intervalos / Ranges

PT-BR:
Intervalos (ranges) representam sequências de valores numéricos. Existem dois tipos: exclusivo (..) e inclusivo (..=).

EN-US:
Ranges represent sequences of numeric values. There are two types: exclusive (..) and inclusive (..=). A stored range has type Range and is backed by a runtime handle, so it can be passed to functions and iterated later without losing its bounds.

// Exclusivo: não inclui o valor final / Exclusive: does not include the final value
let r1 = 0..10
     // 0, 1, 2, ..., 9

// Inclusivo: inclui o valor final / Inclusive: includes the final value
let r2 = 1..=10
    // 1, 2, 3, ..., 10

func soma_intervalo(r: Range) returns int {
    let total = 0
    for i in r {
        total = total + i
    }
    return total
}

let stored = 2..5
let total = soma_intervalo(stored)
 // 2 + 3 + 4 = 9

// Em for loops / In for loops
for i in 0..5 {
    // i = 0, 1, 2, 3, 4
}

for i in 1..=5 {
    // i = 1, 2, 3, 4, 5
}

// Com variáveis / With variables
let inicio = 5
let fim = 10
for i in inicio..fim {
    // i = 5, 6, 7, 8, 9
}

// Empty descending range / intervalo descendente vazio
let empty = 5..2
for i in empty {
    // no iterations
}

std.range exposes handle inspection helpers:

FunctionSignatureMeaning
createfunc(int, int, bool) returns RangeBuild a range handle
lenfunc(Range) returns intCount produced values
atfunc(Range, int) returns intRead value by zero-based index
eqfunc(Range, Range) returns boolCompare range bounds/inclusive flag
startfunc(Range) returns intOriginal start bound
endfunc(Range) returns intOriginal end bound
is_inclusivefunc(Range) returns boolTrue for ..=

Invalid range handles, negative indexes, indexes outside len, invalid create flags, and length overflow fail in the runtime with HOST_STATUS_INVALID_ARGUMENT.