template foo(txn, body: untyped): untyped =
let txn {.gensym.} = 1
body
foo hello:
echo hello
foo hello: # Error: redefinition of hello
echo hello
How do I make the name of entity passed as a template parameter a gensym instead of inject? The gensym pragma seems not working.
I want to mimic the scoping behavior of a for statement. That is, with for hello in ...: body, the variable hello is only accessible in the body.
You cannot gensym the pragma since it is injected as parameter. But you can simply enclose the template body in a block:
template foo(txn, body: untyped): untyped =
block:
let txn = 1
body
foo hello:
echo hello
foo hello:
echo hello