Quirrel4.41.0

modules.reset_static_memos

Defined in module "modules"
from "modules" import reset_static_memos
reset_static_memos(): any

Reset static memo expressions cache.

examples/modules/reset_static_memos-basic.nut
let modules = require("modules")
local calls = 0
function drawId() { calls += 1; return calls }

for (local i = 0; i < 2; i++)
  println("static(drawId()) =", static(drawId()))   // same call site: cached after the first run

modules.reset_static_memos()
println("static(drawId()) =", static(drawId()))      // cache cleared: fresh id
Output:
static(drawId()) = 1
static(drawId()) = 1
static(drawId()) = 2

Return value

null.

Notes

A static expr caches the result of expr the first time that code location runs, then reuses the cached value on every later execution without evaluating expr again. reset_static_memos() discards those cached values (recursively, including nested closures) for every loaded module, so the next execution of each static site recomputes expr and caches the new result.

Call it after replacing script code that a static expression depends on, such as during a hot reload; it is not meant for a per-frame call, only for an infrequent one, since it walks every loaded module's closures.

Example

examples/modules/reset_static_memos.nut
let modules = require("modules")
local calls = 0
function computeOnce() { calls++; return calls }

function useMemo() {
  // Same code location every call, so `static` caches after the first run.
  println("static(computeOnce()) sequence:")
  for (local i = 0; i < 3; i++)
    println(static(computeOnce()))
}

useMemo() // prints 1, 1, 1: computeOnce() ran only on the first pass
modules.reset_static_memos()
useMemo() // prints 2, 2, 2: the cache was cleared, so it recomputed once more
Output:
static(computeOnce()) sequence:
1
1
1
static(computeOnce()) sequence:
2
2
2

See also

on_module_unloadRegister module unload callback. Example: on_module_unload( function(is_app_closing) {println(is_app_closing ? "Closing" : "Soft reloading")} )
get_native_module_namesReturns an array with a list of names of native modules.
modulesmodule index