File: tparam_forwarding.nim

package info (click to toggle)
nim 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,911,644 kB
  • sloc: sh: 24,603; ansic: 1,761; python: 1,492; makefile: 1,013; sql: 298; asm: 141; xml: 13
file content (53 lines) | stat: -rw-r--r-- 1,170 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
discard """
output: '''baz
10
100
1000
a
b
c
x: 1, y: test 1
x: 2, y: test 2
x: 10, y: test 3
x: 4, y: test 4
'''
"""

type
  Foo = object
    x: int

proc stringVarargs*(strings: varargs[string, `$`]): void =
  for s in strings: echo s

proc fooVarargs*(foos: varargs[Foo]) =
  for f in foos: echo f.x

template templateForwarding*(callable: untyped,
                             condition: bool,
                             forwarded: varargs[untyped]): untyped =
  if condition:
    callable(forwarded)

proc procForwarding(args: varargs[string]) =
  stringVarargs(args)

templateForwarding stringVarargs, 17 + 4 < 21, "foo", "bar", 100
templateForwarding stringVarargs, 10 < 21, "baz"

templateForwarding fooVarargs, "test".len > 3, Foo(x: 10), Foo(x: 100), Foo(x: 1000)

procForwarding "a", "b", "c"

proc hasKeywordArgs(x = 10, y = "y") =
  echo "x: ", x, ", y: ", y

proc hasRegularArgs(x: int, y: string) =
  echo "x: ", x, ", y: ", y

templateForwarding(hasRegularArgs, true, 1, "test 1")
templateForwarding hasKeywordArgs, true, 2, "test 2"

templateForwarding(hasKeywordArgs, true, y = "test 3")
templateForwarding hasKeywordArgs, true, y = "test 4", x = 4