tparam_forwarding.nim 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. discard """
  2. output: '''baz
  3. 10
  4. 100
  5. 1000
  6. a
  7. b
  8. c
  9. x: 1, y: test 1
  10. x: 2, y: test 2
  11. x: 10, y: test 3
  12. x: 4, y: test 4
  13. '''
  14. """
  15. type
  16. Foo = object
  17. x: int
  18. proc stringVarargs*(strings: varargs[string, `$`]): void =
  19. for s in strings: echo s
  20. proc fooVarargs*(foos: varargs[Foo]) =
  21. for f in foos: echo f.x
  22. template templateForwarding*(callable: untyped,
  23. condition: bool,
  24. forwarded: varargs[untyped]): untyped =
  25. if condition:
  26. callable(forwarded)
  27. proc procForwarding(args: varargs[string]) =
  28. stringVarargs(args)
  29. templateForwarding stringVarargs, 17 + 4 < 21, "foo", "bar", 100
  30. templateForwarding stringVarargs, 10 < 21, "baz"
  31. templateForwarding fooVarargs, "test".len > 3, Foo(x: 10), Foo(x: 100), Foo(x: 1000)
  32. procForwarding "a", "b", "c"
  33. proc hasKeywordArgs(x = 10, y = "y") =
  34. echo "x: ", x, ", y: ", y
  35. proc hasRegularArgs(x: int, y: string) =
  36. echo "x: ", x, ", y: ", y
  37. templateForwarding(hasRegularArgs, true, 1, "test 1")
  38. templateForwarding hasKeywordArgs, true, 2, "test 2"
  39. templateForwarding(hasKeywordArgs, true, y = "test 3")
  40. templateForwarding hasKeywordArgs, true, y = "test 4", x = 4