Why this template doesn't work? It fails with the error Error: node lacks field: strVal, using a.repr instead of a.str_val fixes it.
import std/macros
macro capt1*(a: typed, body: untyped): untyped =
let a_name = ident(a.str_val); let a_type = get_type_inst a
quote do:
block:
proc create_scope(a_arg: `a_type`) =
let `a_name` = a_arg
`body`
create_scope(`a`)
for v in @[1]:
capt1 v:
echo v
The question is, why almost exactly the same template does work?
import std/macros
macro capt2*(a: typed, b: typed, body: untyped): untyped =
let a_name = ident(a.str_val); let a_type = get_type_inst a
let b_name = ident(b.str_val); let b_type = get_type_inst b
quote do:
block:
proc create_scope(a_arg: `a_type`, b_arg: `b_type`) =
let `a_name` = a_arg; let `b_name` = b_arg
`body`
create_scope(`a`, `b`)
for i, v in @[1]:
capt2 i, v:
echo v
add echo treeRepr a to
macro capt1*(a: typed, body: untyped): untyped =
echo treeRepr a
and you will find out why. Typed code is nowhere near as predictable as untyped code.
I would suggest doing something like this
macro capt1Impl*(aTyped: typed, aUntyped : untyped, body: untyped): untyped
template capt1(a, body : untyped) : untyped =
capt1Impl(a,a,body)
to work around it. Thanks, I also found that a.repr fixes the issue.
It prints Sym "v" for capt2 and HiddenDeref(Sym "v") for capt1.
This is Madness https://i.imgflip.com/7qkpgk.jpg