summary refs log tree commit diff stats
path: root/doc
diff options
context:
space:
mode:
authorCharlesEnding <151328641+CharlesEnding@users.noreply.github.com>2024-09-09 11:46:48 +0200
committerGitHub <noreply@github.com>2024-09-09 11:46:48 +0200
commitfcee829d85605d6d401f15966a283b0041631308 (patch)
tree8ec814baf9934dbae1c1deafa38a662ff6673979 /doc
parent8b895afcb59222723a278f2322f457470d8f1bc4 (diff)
downloadNim-fcee829d85605d6d401f15966a283b0041631308.tar.gz
Adds an example of using ident to name procedures to the macros tutorial (#22973)
As discussed here: https://forum.nim-lang.org/t/10653

---------

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
Diffstat (limited to 'doc')
-rw-r--r--doc/tut3.md30
1 files changed, 30 insertions, 0 deletions
diff --git a/doc/tut3.md b/doc/tut3.md
index 67f49c879..3a55d4790 100644
--- a/doc/tut3.md
+++ b/doc/tut3.md
@@ -322,6 +322,36 @@ used to get this output.
     raise newException(AssertionDefect, $a & " != " & $b)
   ```
 
+
+Going further
+-------------
+
+It is possible to create more complex macros by combining different
+`NimNode` symbols with `quote do:` expressions. For example, you may
+use `newStmtList` to build your macro iteratively, and `ident` in cases
+in which you wish to create an identifier from a string, as shown below.
+
+  ```nim
+  import std/macros
+
+  macro createProcedures() =
+    result = newStmtList()
+
+    for i in 0..<10:
+      let name = ident("myProc" & $i)
+      let content = newLit("I am procedure number #" & $i)
+
+      result.add quote do:
+        proc `name`() =
+          echo `content`
+
+  createProcedures()
+  myProc7()
+  ```
+
+The call to `myProc7` will echo `I am procedure number #7`.
+
+
 With Power Comes Responsibility
 -------------------------------