What are the best practices of meta-programming? And what are anti-patterns?
So far I could find:
You probably said it already, but this idiom served me well:
macro m() =
...
when defined(nimDumpM):
echo repr result
Other random notes:
These practices helped me, don't know whether they are "best" :-)
Keep it obvious: inner templates are probably more readable than constructing and assembling AST nodes "by hand":
macro m(...): untyped =
template tpl(...) =
# people see what is built here
# other logic
result = getAst(tpl(...))
Prefer procs to sub-macros where possible: in case a piece of meta-code should be reuseable, a proc accepting NimNode arguments can do most things a macro can, plus it
Write the macro transformation carefully. Ensure that the lineinfo in the nodes remains useful.
Where can I find any examples of this?