Quirrel4.53.0

debug.get_const_eval_pos

Defined in module "debug"
from "debug" import get_const_eval_pos
get_const_eval_pos(): table|null

Returns the source position of the constant that the compiler evaluates, or null when the code runs at runtime

Return value

A table with these keys, or null when the code runs at runtime:

Notes

The compiler runs a pure function to evaluate a constant, and to fold a call with constant arguments in any expression. A stack trace taken in that run has no frame for the code that uses the constant. This function gives that position, so a diagnostic in a pure function, such as a report of a missing key, can still name the place. Use the position for diagnostics only: the result of a pure function must not depend on it.

Each call in a constant gives its own line, and all code that a call runs gets the position of that call: when a pure function calls another one, both get the same position. But a call with constant arguments in the body of a pure function folds when that body compiles, so it gets its own line in the body.

When the evaluation compiles more code, for example with compilestring, the constants of that code give their own positions, and the outer position comes back when that compile ends.

Example

examples/debug/get_const_eval_pos.nut
from "debug" import get_const_eval_pos

const function [pure] title(key) {
  let pos = get_const_eval_pos()
  if (pos != null)
    println($"{key} is used in {pos.func} at line {pos.line}")
  return key.toupper()
}

const HEADER = title("header")

function caption() {
  return const title("caption")
}

println(HEADER)
println(caption())
let key = "dynamic" // not a constant, so this call of title runs at runtime
println(title(key))
Output:
header is used in __main__ at line 10
caption is used in caption at line 13
HEADER
CAPTION
DYNAMIC

See also

getstackinfosReturns call stack information for the given stack level
debugmodule index