tdiscardable.nim 719 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. discard """
  2. output: '''
  3. tdiscardable
  4. 1
  5. 1
  6. '''
  7. """
  8. echo "tdiscardable"
  9. # Test the discardable pragma
  10. proc p(x, y: int): int {.discardable.} =
  11. return x + y
  12. # test that it is inherited from generic procs too:
  13. proc q[T](x, y: T): T {.discardable.} =
  14. return x + y
  15. p(8, 2)
  16. q[float](0.8, 0.2)
  17. # bug #942
  18. template maybeMod(x: SomeInteger, module: Natural): untyped =
  19. if module > 0: x mod module
  20. else: x
  21. proc foo(b: int):int =
  22. var x = 1
  23. result = x.maybeMod(b) # Works fine
  24. proc bar(b: int):int =
  25. result = 1
  26. result = result.maybeMod(b) # Error: value returned by statement has to be discarded
  27. echo foo(0)
  28. echo bar(0)
  29. # bug #9726
  30. proc foo: (proc: int) =
  31. proc bar: int = 1
  32. return bar
  33. discard foo()