uclosures.nim 538 B

123456789101112131415161718192021222324
  1. # This test is included from within tunittests
  2. import unittest
  3. test "loop variables are captured by ref":
  4. var funcs: seq[proc (): int {.closure.}] = @[]
  5. for i in 0..10:
  6. let ii = i
  7. funcs.add do -> int: return ii * ii
  8. check funcs[0]() == 100
  9. check funcs[3]() == 100
  10. test "loop variables in closureScope are captured by copy":
  11. var funcs: seq[proc (): int {.closure.}] = @[]
  12. for i in 0..10:
  13. closureScope:
  14. let ii = i
  15. funcs.add do -> int: return ii * ii
  16. check funcs[0]() == 0
  17. check funcs[3]() == 9