SplitScript reference / Language / iterator

iterator

keyword

fn name(...) -> iterator T { ... } | (...) -> iterator T => { ... }

Names the type-erased synchronous iterator over T.

Arrays, sets, ranges, maps, adapters, and generator bodies expose the same iterator T type; their representation-specific cursor types are private standard-library details. A function or closure may return any existing iterator with ordinary return. If its body contains yield, the body instead becomes a lazy generator: calling it allocates a cursor without running the body, and each Iterator.next resumes until one yield. Fallthrough or a bare return permanently returns End. Copies alias the same cursor position, and the value implements both Iterator and identity Iterable, so it composes with for, map, and filter. Generators are synchronous: await, retry, value-returning return, and uncaught fallible control are rejected rather than silently changing the iterator protocol.

Examples

Declare a synchronous generator

fn values(end: u32) -> iterator u32 {
    let value = 0u32
    while value < end {
        yield value
        value += 1
    }
}