mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-29 17:34:43 +00:00
added tests/tclosure2
This commit is contained in:
78
tests/run/tclosure2.nim
Normal file
78
tests/run/tclosure2.nim
Normal file
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user