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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
discard """
output: '''
foo88
23 24foo 88
foo88
23 24foo 88
11
int: 108
0
11
1
11
2
11
3
11
4
11
5
11
6
11
7
11
8
11
9
11
11
py
py
py
py
px
6
proc (){.closure, gcsafe, locks: 0.}
'''
"""
block tnestedclosure:
proc main(param: int) =
var foo = 23
proc outer(outerParam: string) =
var outerVar = 88
echo outerParam, outerVar
proc inner() =
block Test:
echo foo, " ", param, outerParam, " ", outerVar
inner()
outer("foo")
# test simple closure within dummy 'main':
proc dummy =
proc main2(param: int) =
var fooB = 23
proc outer(outerParam: string) =
var outerVar = 88
echo outerParam, outerVar
proc inner() =
block Test:
echo fooB, " ", param, outerParam, " ", outerVar
inner()
outer("foo")
main2(24)
dummy()
main(24)
# Jester + async triggered this bug:
proc cbOuter() =
var response = "hohoho"
block:
proc cbIter() =
block:
proc fooIter() =
doAssert response == "hohoho"
fooIter()
cbIter()
cbOuter()
block tnestedproc:
proc p(x, y: int): int =
result = x + y
echo p((proc (): int =
var x = 7
return x)(),
(proc (): int = return 4)())
block deeplynested:
# bug #4070
proc id(f: (proc())): auto =
return f
proc foo(myinteger: int): (iterator(): int) =
return iterator(): int {.closure.} =
proc bar() =
proc kk() =
echo "int: ", myinteger
kk()
id(bar)()
discard foo(108)()
block tclosure2:
when true:
proc ax =
for xxxx in 0..9:
var i = 0
proc bx =
if i > 10:
echo xxxx
return
i += 1
#for j in 0 .. 0: echo i
bx()
bx()
echo i
ax()
when true:
proc accumulator(start: int): (proc(): int {.closure.}) =
var x = start-1
#let dummy = proc =
# discard start
result = proc (): int =
#var x = 9
for i in 0 .. 0: x = x + 1
return x
var a = accumulator(3)
let b = accumulator(4)
echo a() + b() + a()
proc outer =
proc py() =
# no closure here:
for i in 0..3: echo "py"
py()
outer()
when true:
proc outer2 =
var errorValue = 3
proc fac[T](n: T): T =
if n < 0: result = errorValue
elif n <= 1: result = 1
else: result = n * fac(n-1)
proc px() {.closure.} =
echo "px"
proc py() {.closure.} =
echo "py"
let
mapping = {
"abc": px,
"xyz": py
}
mapping[0][1]()
echo fac(3)
outer2()
# bug #5688
import typetraits
proc myDiscard[T](a: T) = discard
proc foo() =
let a = 5
let f = (proc() =
myDiscard (proc() = echo a)
)
echo name(typeof(f))
foo()
|