tunderscoreparam.nim 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. import std/[assertions, sequtils]
  5. proc test() =
  6. block:
  7. proc ok(_, _, a: int): int =
  8. doAssert not compiles(_)
  9. a
  10. doassert ok(4, 2, 5) == 5
  11. block:
  12. proc ok(_: int, _: int, a: int): int = a
  13. doAssert ok(4, 2, 5) == 5
  14. block:
  15. proc ok(_: int, _: float, a: int): int = a
  16. doAssert ok(1, 2.0, 5) == 5
  17. block:
  18. proc ok(_: int, _: float, _: string, a: int): int = a
  19. doAssert ok(1, 2.6, "5", 5) == 5
  20. block:
  21. proc ok[T](_, _, a: T): T =
  22. doAssert not compiles(_)
  23. a
  24. doAssert ok(4, 2, 5) == 5
  25. doAssert ok("a", "b", "c") == "c"
  26. doAssert not compiles(ok(1, 2, "a"))
  27. block:
  28. let ok = proc (_, _, a: int): int =
  29. doAssert not compiles(_)
  30. a
  31. doAssert ok(4, 2, 5) == 5
  32. block:
  33. proc foo(lam: proc (_, _, a: int): int): int =
  34. lam(4, 2, 5)
  35. doAssert foo(proc (_, _, a: auto): auto =
  36. doAssert not compiles(_)
  37. a) == 5
  38. block:
  39. iterator fn(_, _: int, c: int): int = yield c
  40. doAssert toSeq(fn(1,2,3)) == @[3]
  41. block:
  42. template ok(_, _, a: int): int = a
  43. doAssert ok(4, 2, 5) == 5
  44. block:
  45. doAssert not (compiles do:
  46. template bad(_: int): int = _
  47. discard bad(3))
  48. block:
  49. template ok(_: int, _: int, a: int): int = a
  50. doAssert ok(4, 2, 5) == 5
  51. block:
  52. template ok(_: int, _: float, a: int): int = a
  53. doAssert ok(1, 2.0, 5) == 5
  54. block:
  55. template ok(_: int, _: float, _: string, a: int): int = a
  56. doAssert ok(1, 2.6, "5", 5) == 5
  57. block:
  58. template main2() =
  59. iterator fn(_, _: int, c: int): int = yield c
  60. main2()
  61. block:
  62. template main =
  63. proc foo(_: int) =
  64. let a = _
  65. doAssert not compiles(main())
  66. block: # generic params
  67. doAssert not (compiles do:
  68. proc foo[_](t: typedesc[_]): seq[_] = @[default(_)]
  69. doAssert foo[int]() == 0)
  70. block:
  71. proc foo[_, _](): int = 123
  72. doAssert foo[int, bool]() == 123
  73. block:
  74. proc foo[T; U](_: typedesc[T]; _: typedesc[U]): (T, U) = (default(T), default(U))
  75. doAssert foo(int, bool) == (0, false)
  76. proc closureTest() =
  77. var x = 0
  78. block:
  79. proc foo(_, _: int) = x += 5
  80. foo(1, 2)
  81. doAssert x == 5
  82. block:
  83. proc foo(_: int, _: float) = x += 5
  84. foo(1, 2)
  85. doAssert x == 10
  86. block:
  87. proc foo(_: int, _: float, _: string) = x += 5
  88. foo(1, 2, "5")
  89. doAssert x == 15
  90. static: test()
  91. test()
  92. when not defined(js):
  93. static: closureTest()
  94. closureTest()