summary refs log tree commit diff stats
path: root/doc
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2019-09-05 11:56:32 +0200
committerGitHub <noreply@github.com>2019-09-05 11:56:32 +0200
commita5e2db2ac53aa61d0e050ee6487e38c647eb442d (patch)
tree5544badb2d7ad8da252e16386bdf27998d076161 /doc
parent58bcf6cd46fe9108f2dced59c612153d2951aee2 (diff)
downloadNim-a5e2db2ac53aa61d0e050ee6487e38c647eb442d.tar.gz
allows access to .compileTime vars at runtime (#12128)
Diffstat (limited to 'doc')
-rw-r--r--doc/manual.rst25
1 files changed, 25 insertions, 0 deletions
diff --git a/doc/manual.rst b/doc/manual.rst
index a8326d94d..3c194b1d2 100644
--- a/doc/manual.rst
+++ b/doc/manual.rst
@@ -5928,6 +5928,31 @@ Is the same as:
   proc astHelper(n: NimNode): NimNode {.compileTime.} =
     result = n
 
+``compileTime`` variables are available at runtime too. This simplifies certain
+idioms where variables are filled at compile-time (for example, lookup tables)
+but accessed at runtime:
+
+.. code-block:: nim
+    :test: "nim c -r $1"
+
+  import macros
+
+  var nameToProc {.compileTime.}: seq[(string, proc (): string {.nimcall.})]
+
+  macro registerProc(p: untyped): untyped =
+    result = newTree(nnkStmtList, p)
+
+    let procName = p[0]
+    let procNameAsStr = $p[0]
+    result.add quote do:
+      nameToProc.add((`procNameAsStr`, `procName`))
+
+  proc foo: string {.registerProc.} = "foo"
+  proc bar: string {.registerProc.} = "bar"
+  proc baz: string {.registerProc.} = "baz"
+
+  doAssert nameToProc[2][1]() == "baz"
+
 
 noReturn pragma
 ---------------