On page 34 of the book there is a template
template withColor(col:Color; body:untyped) =
let colorContext {.inject.} =col
body
....
withColor(Blue) :
.....
Unfortunately the template withColor can only be used a single time in the name space. A second invocation of it produces a redefinition error of colorContext.
How can one improve this example without this limitation?
Thanks, Helmut
Here's an example of something that makes new variables injected into scope, instead of the same variable over and over again. You can adapt it this technique to achieve what you are looking for.
template makeVar(varname: untyped; varvalue: typed) =
let `varname` {.inject.} = varvalue
makeVar(myVar, 34)
makeVar(myVar2, true)
echo myVar
echo myVar2
Or just scope inside of the template:
template withColor(col:Color; body:untyped) =
block:
let colorContext {.inject.} = col
body
....
withColor(Blue) :
.....