I don't know if a template is the right way to do it, but this is what I'm trying to do:
template doSth(i: int): untyped =
if i>=1:
let x = "X"
if i>=2:
let y = "Y"
.....
proc someOtherProc() =
doSth(1)
echo fmt("x = {x}")
Basically, I want to have an "x" variable in someOtherProc.
How could I do it?
You sure can!
Use inject pragma. Here's an example: https://github.com/moigagoo/norm/blob/develop/src/norm/sqlite.nim#L36
maybe with static, when and inject:
import strformat
# https://forum.nim-lang.org/t/6616#40996
#[
template createVar(name, value) =
var name {.inject.} = value
createVar(variable, 100)
]#
template doSth(i: static[int]): untyped =
when i >= 1:
let x {.inject.} = "X"
when i >= 2:
let y {.inject.} = "Y"
proc someOtherProc =
doSth(1)
echo fmt("x = {x}")
someOtherProc()
Stefan, thanks!
Great solution.