tsimpleclosure.nim 921 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. discard """
  2. cmd: '''nim c -d:nimAllocStats --newruntime $file'''
  3. output: '''a b
  4. 70
  5. hello
  6. hello
  7. hello
  8. (allocCount: 3, deallocCount: 3)'''
  9. """
  10. import system / ansi_c
  11. proc main(): owned(proc()) =
  12. var a = "a"
  13. var b = "b"
  14. result = proc() =
  15. echo a, " ", b
  16. proc foo(f: (iterator(): int)) =
  17. for i in f(): echo i
  18. proc wrap =
  19. let p = main()
  20. p()
  21. let fIt = iterator(): int = yield 70
  22. foo fIt
  23. wrap()
  24. # bug #11533
  25. proc say = echo "hello"
  26. # Error: internal error: genAssignment: tyNil
  27. var err0: proc() = say
  28. err0()
  29. var ok0: proc()
  30. ok0 = say
  31. ok0()
  32. var ok1 = say
  33. ok1()
  34. when false:
  35. # bug #12443
  36. func newStringIterator(s: string): owned(iterator(): char) =
  37. result = iterator(): char =
  38. var pos = 0
  39. while pos < s.len:
  40. yield s[pos]
  41. inc pos
  42. proc stringIter() =
  43. let si = newStringIterator("foo")
  44. for i in si():
  45. echo i
  46. stringIter()
  47. echo getAllocStats()