I have been able to make this work using `untyped` and wrapping all parameters
inside a single node that is parsed as an object declaration.
The macro is mostly the same:
macro attrs*(xs: untyped): Attrs =
let a = !"a"
var body = quote do:
var `a` {.noinit.}: Attrs
{.emit: "`a` = {};" .}
for x in xs:
if x.kind == nnkExprColonExpr:
let
k = x[0]
v = x[1]
body.add(quote do:
`a`.`k` = `v`
)
body.add(quote do:
return `a`
)
result = quote do:
proc inner(): Attrs {.gensym.} =
`body`
inner()
Unfortunately the usage site becomes a little cumbersome. One has to call it
like this:
attrs(only(key: "hi", placeholder: "foo"))
Apart from the `only` name (any other symbol would do, actually) it is exactly
what I wanted.
Any suggestion for improvements?