re: https://forum.nim-lang.org/t/7945 maybe there's a better way but my answer required that a template be expanded at global scope, in order to declare a global.
yes, .global might be an answer for that problem, but new question:
i know we have isMainModule, which is true when in the main compilation unit, but is there anything that could detect whether something is at global scope or not?
You could also try the expandMacros macro from the macros module. It works on templates as well. It should output to the terminal, on compilation, whatever you put into the expandMacros block with all of the templates and macros expanded out to the resulting code. Sometimes seeing what exact code is being generated can help you to understand why the behavior is not as anticipated.
Example:
import macros
template add(a: untyped; b: untyped): untyped = a + b
let
a = 1
b = 2
expandMacros:
let c = add(a, b)
echo c
Output:
Hint: used config file '/obfuscated/.choosenim/toolchains/nim-1.4.6/config/nim.cfg' [Conf]
Hint: used config file '/obfuscated/.choosenim/toolchains/nim-1.4.6/config/config.nims' [Conf]
.....
let c = a + b
echo [c]
CC: stdlib_io.nim
CC: stdlib_system.nim
CC: example.nim
Hint: [Link]
Hint: 24180 lines; 5.483s; 25.496MiB peakmem; Debug build; proj: /obfuscated/example.nim; out: /obfuscated/example [SuccessX]
Hint: /obfuscated/example [Exec]
3