11.7. static_let macro

The STATIC_LET module implements the static_let pattern — local variables that persist across function calls, similar to C static variables. The variable is initialized once on first call and retains its value in subsequent invocations.

All functions and symbols are in “static_let” module, use require to get access to it.

require daslib/static_let

Example:

require daslib/static_let

    def counter() : int {
        static_let() {
            var count = 0
        }
        count ++
        return count
    }

    [export]
    def main() {
        print("{counter()}\n")
        print("{counter()}\n")
        print("{counter()}\n")
    }
    // output:
    // 1
    // 2
    // 3

11.7.1. Function annotations

static_let::StaticLetMacro

This macro implements the static_let and static_let_finalize functions.

11.7.2. Static variable declarations

11.7.2.1. static_let

static_let::static_let(name: string; blk: block<():void>)

Given a scope with the variable declarations, this function will make those variables global. Variable will be renamed under the hood, and all local access to it will be renamed as well.

Arguments
  • name : string

  • blk : block<void>

static_let::static_let(blk: block<():void>)

static_let::static_let_finalize(blk: block<():void>)

This is very similar to regular static_let, but additionally the variable will be deleted on the context shutdown.

Arguments
  • blk : block<void>