tvarargslen.nim 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. discard """
  2. output: '''
  3. tvarargslen.nim:35:2 (1, 2)
  4. tvarargslen.nim:36:2 12
  5. tvarargslen.nim:37:2 1
  6. tvarargslen.nim:38:8
  7. done
  8. '''
  9. """
  10. ## line 10
  11. template myecho*(a: varargs[untyped]) =
  12. ## shows a useful debugging echo-like proc that is dependency-free (no dependency
  13. ## on macros.nim) so can be used in more contexts
  14. const info = instantiationInfo(-1, false)
  15. const loc = info.filename & ":" & $info.line & ":" & $info.column & " "
  16. when varargsLen(a) > 0:
  17. echo(loc, a)
  18. else:
  19. echo(loc)
  20. template fun*(a: varargs[untyped]): untyped =
  21. varargsLen(a)
  22. template fun2*(a: varargs[typed]): untyped =
  23. a.varargsLen
  24. template fun3*(a: varargs[int]): untyped =
  25. a.varargsLen
  26. template fun4*(a: varargs[untyped]): untyped =
  27. len(a)
  28. proc main()=
  29. myecho (1, 2)
  30. myecho 1, 2
  31. myecho 1
  32. myecho()
  33. doAssert fun() == 0
  34. doAssert fun('a') == 1
  35. doAssert fun("asdf", 1) == 2
  36. doAssert fun2() == 0
  37. doAssert fun2('a') == 1
  38. doAssert fun2("asdf", 1) == 2
  39. doAssert fun3() == 0
  40. doAssert fun3(10) == 1
  41. doAssert fun3(10, 11) == 2
  42. ## shows why `varargsLen` can't be named `len`
  43. doAssert fun4("abcdef") == len("abcdef")
  44. ## workaround for BUG:D20191218T171447 whereby if testament expected output ends
  45. ## in space, testament strips it from expected output but not actual output,
  46. ## which leads to a mismatch when running test via megatest
  47. echo "done"
  48. main()