Skip to content

Commit

Permalink
Fix capture for object types (nim-lang#13315)
Browse files Browse the repository at this point in the history
* Fix capture for object|tuple|... types

* Add test case
  • Loading branch information
Clyybber authored Feb 2, 2020
1 parent 45a5c64 commit d43e5be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/pure/sugar.nim
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ macro capture*(locals: openArray[typed], body: untyped): untyped {.since: (1, 1)
## echo r[0] & ", or " & r[1] # output: to be, or not to be
var params = @[newIdentNode("auto")]
for arg in locals:
params.add(newIdentDefs(ident(arg.strVal), freshIdentNodes getTypeImpl arg))
params.add(newIdentDefs(ident(arg.strVal), freshIdentNodes getTypeInst arg))
result = newNimNode(nnkCall)
result.add(newProc(newEmptyNode(), params, body, nnkProcDef))
for arg in locals: result.add(arg)
Expand Down
18 changes: 16 additions & 2 deletions tests/closure/tcapture.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
discard """
output: '''
to be, or not to be'''
to be, or not to be
(v: 1)
(v: 1)
'''
joinable: false
"""

Expand All @@ -9,4 +12,15 @@ import sequtils, sugar
let m = @[proc (s: string): string = "to " & s, proc (s: string): string = "not to " & s]
var l = m.mapIt(capture([it], proc (s: string): string = it(s)))
let r = l.mapIt(it("be"))
echo r[0] & ", or " & r[1]
echo r[0] & ", or " & r[1]

type O = object
v: int
var o = O(v: 1)
var execute: proc()
capture [o]:
execute = proc() =
echo o
execute()
o.v = -1
execute()

0 comments on commit d43e5be

Please sign in to comment.