← nimrepo

macro in templates

b9fe98f574b7213e9e1c1fc1a618fe3333fd6a2d · ovidiucs · Feb 19, 2019 09:51

+11 −2 · 1 file changed

  • templates_stuff.nim +11 −2
diff --git a/templates_stuff.nim b/templates_stuff.nim
index ad36633..2332fd9 100644
--- a/templates_stuff.nim
+++ b/templates_stuff.nim
@@ -6,7 +6,7 @@ from os import sleep
 ## * A template operates on Nim's abstract syntax trees.
 ## * A template generates procedures.
 ## * A template can accept multiple parameters such as variables or type names.
-## * A template is injected into the main scope as is.
+## * A template however will inject its content at the place where you call it as if you copy pasted its content there.
 ## * A tempalte is processed in the semantic pass of the compiler.
 ## * A template in nim is a simple declarative macro
 ## * It provides a substitution mechanism that allows you to substitute
@@ -22,6 +22,7 @@ from os import sleep
 ## * A template is a way to use one function in multiple scenarios.
 ## * Document precisely the variables that are injected by the template.
 
+
 ## Example:
 ##
 ## .. code-block:: Nim
@@ -57,7 +58,7 @@ foo():
 template `!=` (a,b: untyped): untyped =
    not (a == b)
 
-assert(5 != 6)
+doAssert(5 != 6)
 
 ## Details about types:
 ##
@@ -142,5 +143,13 @@ template repeat (statements: untyped) =
 #  echo("Whoaa!")
 #  sleep(1000)
 
+## Tenplate with macros
+template repeatM(n: static[Natural], body: untyped): untyped =
+  for _ in 0 ..< n:
+    body
+
+repeatM(4):
+  echo "Hi"
+  sleep(505)