tdiscardable.nim 993 B

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