tvoid.nim 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. discard """
  2. output: '''12
  3. empty
  4. he, no return type;
  5. abc a string
  6. ha'''
  7. """
  8. proc ReturnT[T](x: T): T =
  9. when T is void:
  10. echo "he, no return type;"
  11. else:
  12. result = x & " a string"
  13. proc nothing(x, y: void): void =
  14. echo "ha"
  15. proc callProc[T](p: proc (x: T) {.nimcall.}, x: T) =
  16. when T is void:
  17. p()
  18. else:
  19. p(x)
  20. proc intProc(x: int) =
  21. echo x
  22. proc emptyProc() =
  23. echo "empty"
  24. callProc[int](intProc, 12)
  25. callProc[void](emptyProc)
  26. ReturnT[void]()
  27. echo ReturnT[string]("abc")
  28. nothing()
  29. block: # typeof(stmt)
  30. proc fn1(): auto =
  31. discard
  32. proc fn2(): auto =
  33. 1
  34. doAssert type(fn1()) is void
  35. doAssert typeof(fn1()) is void
  36. doAssert typeof(fn1()) isnot int
  37. doAssert type(fn2()) isnot void
  38. doAssert typeof(fn2()) isnot void
  39. when typeof(fn1()) is void: discard
  40. else: doAssert false
  41. doAssert typeof(1+1) is int
  42. doAssert typeof((discard)) is void
  43. type A1 = typeof(fn1())
  44. doAssert A1 is void
  45. type A2 = type(fn1())
  46. doAssert A2 is void
  47. doAssert A2 is A1
  48. when false:
  49. # xxx: MCS/UFCS doesn't work here: Error: expression 'fn1()' has no type (or is ambiguous)
  50. type A3 = fn1().type
  51. proc bar[T](a: T): string = $T
  52. doAssert bar(1) == "int"
  53. doAssert bar(fn1()) == "void"
  54. proc bar2[T](a: T): bool = T is void
  55. doAssert not bar2(1)
  56. doAssert bar2(fn1())
  57. block:
  58. proc bar3[T](a: T): T = a
  59. let a1 = bar3(1)
  60. doAssert compiles(block:
  61. let a1 = bar3(fn2()))
  62. doAssert not compiles(block:
  63. let a2 = bar3(fn1()))
  64. doAssert compiles(block: bar3(fn1()))
  65. doAssert compiles(bar3(fn1()))
  66. doAssert typeof(bar3(fn1())) is void
  67. doAssert not compiles(sizeof(bar3(fn1())))
  68. block:
  69. var a = 1
  70. doAssert typeof((a = 2)) is void
  71. doAssert typeof((a = 2; a = 3)) is void
  72. doAssert typeof(block:
  73. a = 2; a = 3) is void
  74. block:
  75. var a = 1
  76. template bad1 = echo (a; a = 2)
  77. doAssert not compiles(bad1())
  78. block:
  79. template bad2 = echo (nonexistent; discard)
  80. doAssert not compiles(bad2())