summary refs log tree commit diff stats
path: root/doc/manual.rst
diff options
context:
space:
mode:
Diffstat (limited to 'doc/manual.rst')
-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
 ---------------
e previous revision' href='/ahoang/Nim/blame/rod/magicsys.nim?h=devel&id=90119066adf6a9a2e8d779d4955637c6dd99aba3'>^
2df9b442c ^
e25474154 ^


2900ceae3 ^
e25474154 ^


7014d0c5c ^

e25474154 ^





7916b1f9a ^
e25474154 ^

92b8fac94 ^
8090b6c02 ^



e25474154 ^
dbf9117c5 ^
e25474154 ^
23ef565a3 ^


92b8fac94 ^
23ef565a3 ^


92b8fac94 ^
23ef565a3 ^



ce454fb83 ^
e25474154 ^










e2d38a57e ^




e25474154 ^

4d9b2f671 ^
e2d38a57e ^
e25474154 ^


438703f59 ^
e25474154 ^

92b8fac94 ^
e25474154 ^

92b8fac94 ^

2900ceae3 ^
4fbba0a65 ^

2900ceae3 ^
c67520a7c ^








4fbba0a65 ^





2900ceae3 ^


4fbba0a65 ^





7056ceda6 ^




121d4e0fc ^
7056ceda6 ^



121d4e0fc ^
2df9b442c ^
121d4e0fc ^
23ef565a3 ^


121d4e0fc ^
4fbba0a65 ^

b731e6ef1 ^
4fbba0a65 ^






















2df9b442c ^
2900ceae3 ^
e25474154 ^
922e216b8 ^
2df9b442c ^
dbf9117c5 ^
92b8fac94 ^
dbf9117c5 ^
e25474154 ^

dbf9117c5 ^


e25474154 ^

2df9b442c ^
e25474154 ^
2df9b442c ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181