For example, the following code doesn't compile:
template t2() =
template t1() = res.add "hello"
var res = ""
t1()
echo res
proc p1() =
t2()
when isMainModule:
p1()
Run
The compiler out:
:\Tool\Nim\tmp\a59.nim(9, 6) template/generic instantiation of `t2` from
here
D:\Tool\Nim\tmp\a59.nim(5, 6) template/generic instantiation of `t1` from
here
D:\Tool\Nim\tmp\a59.nim(2, 20) Error: undeclared identifier: 'res'
Run
One way fix this is use `var res {.inject.} = ""`.
It is very annoying, when we chang the `template t2` to `proc t2`, the code is
fine, because the t2 will be expanded in proc t2 context. When an template call
another template, we have to pass all variable as parameter to caller template.