LEARN VOID / DATA & BEHAVIOR

Functions & closures

Let a function carry the state it needs.

closures.voidDownload source ↓
func counter(start: i32) -> func() -> i32:
    var number = start
    return func() -> i32:
        number += 1
        return number

func entry(value: i32) -> i32:
    var next = counter(value)
    var alias = next
    var first = next()
    return first + alias()
Input 5Expected result 13

Return a function

counter returns a function with signature func() -> i32. The returned function captures number and keeps that binding alive after counter returns.

Share a captured binding intentionally

next and alias refer to the same closure state. With input 5, the first call returns 6 and the next returns 7, for a final result of 13. Ordinary value assignments outside closure capture still keep their usual copy semantics.

Use precise function boundaries

Function values have typed positional signatures. Named functions, supported method references and compatible callable structs can be used as callbacks. Escaping closures retain the managed references they capture.