tsimpleclosure.nim 696 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. discard """
  2. cmd: '''nim c --newruntime $file'''
  3. output: '''a b
  4. 70
  5. hello
  6. hello
  7. hello
  8. 2 2 alloc/dealloc pairs: 0'''
  9. """
  10. import core / allocators
  11. import system / ansi_c
  12. proc main(): owned(proc()) =
  13. var a = "a"
  14. var b = "b"
  15. result = proc() =
  16. echo a, " ", b
  17. proc foo(f: (iterator(): int)) =
  18. for i in f(): echo i
  19. proc wrap =
  20. let p = main()
  21. p()
  22. let fIt = iterator(): int = yield 70
  23. foo fIt
  24. wrap()
  25. # bug #11533
  26. proc say = echo "hello"
  27. # Error: internal error: genAssignment: tyNil
  28. var err0: proc() = say
  29. err0()
  30. var ok0: proc()
  31. ok0 = say
  32. ok0()
  33. var ok1 = say
  34. ok1()
  35. let (a, d) = allocCounters()
  36. discard cprintf("%ld %ld alloc/dealloc pairs: %ld\n", a, d, system.allocs)