diff options
author | Araq <rumpf_a@web.de> | 2012-02-08 22:58:27 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2012-02-08 22:58:27 +0100 |
commit | ca16db60947ee776a0fc3712cae429e7691fe7cb (patch) | |
tree | 6394ae052a18905d46bbfaf00bbd6e1a1c32af56 /tests | |
parent | f3f3fc01aa4d7ff36c8e5865d5e7a234636c42b8 (diff) | |
download | Nim-ca16db60947ee776a0fc3712cae429e7691fe7cb.tar.gz |
added tests/tclosure2
Diffstat (limited to 'tests')
-rw-r--r-- | tests/run/tclosure2.nim | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/run/tclosure2.nim b/tests/run/tclosure2.nim new file mode 100644 index 000000000..c71cc9f1c --- /dev/null +++ b/tests/run/tclosure2.nim @@ -0,0 +1,78 @@ +discard """ + output: '''1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +11 +py +py +py +py''' +""" + +when true: + proc ax = + var i = 0 + proc bx = + if i > 10: return + i += 1 + for j in 0 .. 0: echo i + bx() + + bx() + + 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 false: + proc outer = + proc px() {.closure.} = + echo "px" + + proc py() {.closure.} = + echo "py" + + const + mapping = { + "abc": px, + "xyz": py + } + mapping[0][1]() + + + outer() + |