Good catch! I did not even noticed that my solution was not correct. Templates 
are _so_ mysterious when you use them out of their sweet spot...

Learning meta-programming in Nim is not easy. First, there are two ways to 
doing it: templates and macros. ¸The first one seems like programming with 
usual procs but is limited in what it can do, and the other one requires to 
learn working with AST but is more powerful. Which one you choose for your task 
is not clearly explained in the documentation, as it is not that current to 
find multiple meta-programming layers in a language. But the path to 
meta-programming is bordered with pitfalls (and I'm quite good at falling into 
all I encounter!). And the documentation is quite terse and not really 
didactical on the subject. Presently, I'm using:

  * Nim manual starting from 
[https://nim-lang.org/docs/manual.html#templates](https://nim-lang.org/docs/manual.html#templates)
  * Nim tutorial part 3: 
[https://nim-lang.org/docs/tut3.html](https://nim-lang.org/docs/tut3.html)
  * A bit outdated, but at least with examples: 
[https://hookrace.net/blog/introduction-to-metaprogramming-in-nim](https://hookrace.net/blog/introduction-to-metaprogramming-in-nim)/
  * And of course the macros library: 
[https://nim-lang.org/docs/macros.html](https://nim-lang.org/docs/macros.html)



I try to document what I find for others, either in that forum or on Nim wiki. 
But there are still many subjects I haven't enough experience on like:

  * How to debug macros and templates? Particularly when you encounter strange 
error message like `Error: cannot attach a custom pragma to '<put a name 
here>'`...
  * When to use `template` vs when to use `macro`? A decision tree for the best 
tool would be useful.
  * Most common pitfalls when doing meta-programming. For instance, calling a 
macro inside a macro. Overloading with typed/untyped macros. Etc.



Perhaps, the [Macros Tutorial](https://nim-lang.org/docs/tut3.html) could be 
improved by experienced users. Referring beginners to @krux02 projects in [More 
Examples](https://nim-lang.org/docs/tut3.html#more-examples) is quite a steep 
path... Easier examples would be welcome. And no @mratsim, the multibody macro 
is not a simple example!

I've followed @Araq's advice and converted the most complex templates to 
macros. I still have some work to do to be sure that the recognized DSL syntax 
is robust...

Reply via email to