11.8. 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
declaration is promoted to module scope under a mangled name, so it is
initialized once when the context starts (not lazily on first call) and retains
its value across calls. static_let_finalize additionally deletes the
variable on context shutdown.
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.8.1. Function annotations
- StaticLetMacro
This macro implements the static_let and static_let_finalize functions.
11.8.2. Static variable declarations
11.8.2.1. static_let
- static_let(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:
blk : block<void>
- static_let(name: string; blk: block<():void> )
- 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>