diff options
author | Clyybber <darkmine956@gmail.com> | 2020-02-02 13:52:21 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-02 13:52:21 +0100 |
commit | d43e5bef3909c791ef41ba8cad5decc164170ebe (patch) | |
tree | b26879b13bc9aa5761c09aae0c5e9f4c8afaedc6 | |
parent | 45a5c64c9a01cb198450fa1a7b108710241c8768 (diff) | |
download | Nim-d43e5bef3909c791ef41ba8cad5decc164170ebe.tar.gz |
Fix capture for object types (#13315)
* Fix capture for object|tuple|... types * Add test case
-rw-r--r-- | lib/pure/sugar.nim | 2 | ||||
-rw-r--r-- | tests/closure/tcapture.nim | 18 |
2 files changed, 17 insertions, 3 deletions
diff --git a/lib/pure/sugar.nim b/lib/pure/sugar.nim index 5719e8dd7..fa28ffcf4 100644 --- a/lib/pure/sugar.nim +++ b/lib/pure/sugar.nim @@ -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) diff --git a/tests/closure/tcapture.nim b/tests/closure/tcapture.nim index 304a76285..ff3490f07 100644 --- a/tests/closure/tcapture.nim +++ b/tests/closure/tcapture.nim @@ -1,6 +1,9 @@ discard """ output: ''' -to be, or not to be''' +to be, or not to be +(v: 1) +(v: 1) +''' joinable: false """ @@ -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] \ No newline at end of file +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() |