tnosideeffect.nim 514 B

12345678910111213141516171819202122232425
  1. block: # `.noSideEffect`
  2. func foo(bar: proc(): int): int {.effectsOf: bar.} = bar()
  3. var count = 0
  4. proc fn1(): int = 1
  5. proc fn2(): int = (count.inc; count)
  6. template accept(body) =
  7. doAssert compiles(block:
  8. body)
  9. template reject(body) =
  10. doAssert not compiles(block:
  11. body)
  12. accept:
  13. func fun1() = discard foo(fn1)
  14. reject:
  15. func fun1() = discard foo(fn2)
  16. var foo2: type(foo) = foo
  17. accept:
  18. func main() = discard foo(fn1)
  19. reject:
  20. func main() = discard foo2(fn1)