summary refs log tree commit diff stats
path: root/tests/closure/tcapture.nim
blob: dafc447392922d960b744985b3a63c91c242e4e4 (plain) (blame)
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
discard """
  output: '''
to be, or not to be
(v: 1)
(w: -1)
(v: 1)
(w: -1)
'''
  joinable: false
"""

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]

type
  O = object
    v: int
  U = object
    w: int
var o = O(v: 1)
var u = U(w: -1)
var execute: proc()
capture o, u:
  execute = proc() =
    echo o
    echo u
execute()
o.v = -1
u.w = 1
execute()