-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncblock.nim
49 lines (42 loc) · 1.45 KB
/
funcblock.nim
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
##[Implements a `block` that acts like a `func`. That is, a `block` that
returns a value and has no side effects.]##
template funcBlock*(returning, body: untyped): untyped =
## A specialization of the `block` keyword.
## Returns a value of type `returning`, and has no side effects.
runnableExamples:
proc myProc(): string =
let a = funcBlock(int):
# Each funcBlock has its own internal result variable.
result = 0
for c in '0' .. '9':
result += 1
assert result == 10
# After exiting the funcBlock's scope, the old result variable is
# forgotten and stored into the declared variable.
assert result == ""
assert a == 10
assert myProc() == ""
block:
func insideFuncBlock(): returning =
body
insideFuncBlock()
template procBlock*(returning, body: untyped): untyped =
## A specialization of the `block` keyword.
## Returns a value of type `returning`.
runnableExamples:
proc myProc(): string =
let a = procBlock(int):
# Each procBlock has its own internal result variable.
result = 0
for c in '0' .. '9':
result += 1
assert result == 10
# After exiting the procBlock's scope, the old result variable is
# forgotten and stored into the declared variable.
assert result == ""
assert a == 10
assert myProc() == ""
block:
proc insideFuncBlock(): returning =
body
insideFuncBlock()