Skip to content

Commit d43e5be

Browse files
authored
Fix capture for object types (nim-lang#13315)
* Fix capture for object|tuple|... types * Add test case
1 parent 45a5c64 commit d43e5be

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Diff for: lib/pure/sugar.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ macro capture*(locals: openArray[typed], body: untyped): untyped {.since: (1, 1)
184184
## echo r[0] & ", or " & r[1] # output: to be, or not to be
185185
var params = @[newIdentNode("auto")]
186186
for arg in locals:
187-
params.add(newIdentDefs(ident(arg.strVal), freshIdentNodes getTypeImpl arg))
187+
params.add(newIdentDefs(ident(arg.strVal), freshIdentNodes getTypeInst arg))
188188
result = newNimNode(nnkCall)
189189
result.add(newProc(newEmptyNode(), params, body, nnkProcDef))
190190
for arg in locals: result.add(arg)

Diff for: tests/closure/tcapture.nim

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
discard """
22
output: '''
3-
to be, or not to be'''
3+
to be, or not to be
4+
(v: 1)
5+
(v: 1)
6+
'''
47
joinable: false
58
"""
69

@@ -9,4 +12,15 @@ import sequtils, sugar
912
let m = @[proc (s: string): string = "to " & s, proc (s: string): string = "not to " & s]
1013
var l = m.mapIt(capture([it], proc (s: string): string = it(s)))
1114
let r = l.mapIt(it("be"))
12-
echo r[0] & ", or " & r[1]
15+
echo r[0] & ", or " & r[1]
16+
17+
type O = object
18+
v: int
19+
var o = O(v: 1)
20+
var execute: proc()
21+
capture [o]:
22+
execute = proc() =
23+
echo o
24+
execute()
25+
o.v = -1
26+
execute()

0 commit comments

Comments
 (0)