File: tstaticparamsmacro.nim

package info (click to toggle)
nim 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 462,356 kB
  • sloc: sh: 11,089; ansic: 4,699; makefile: 706; python: 309; sql: 297; asm: 141; xml: 13
file content (74 lines) | stat: -rw-r--r-- 1,145 bytes parent folder | download
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
discard """
  msg: '''letters
aa
bb
numbers
11
22
AST a
[(11, 22), (33, 44)]
AST b
(e: [55, 66], f: [77, 88])
55
10
20Test
20
'''
"""

import macros

type
  TConfig = tuple
    letters: seq[string]
    numbers:seq[int]

const data: Tconfig = (@["aa", "bb"], @[11, 22])

macro mymacro(data: static[TConfig]): untyped =
  echo "letters"
  for s in items(data.letters):
    echo s
  echo "numbers"
  for n in items(data.numbers):
    echo n

mymacro(data)

type
  Ta = seq[tuple[c:int, d:int]]
  Tb = tuple[e:seq[int], f:seq[int]]

const
  a : Ta = @[(11, 22), (33, 44)]
  b : Tb = (@[55,66], @[77, 88])

macro mA(data: static[Ta]): untyped =
  echo "AST a \n", repr(data)

macro mB(data: static[Tb]): untyped =
  echo "AST b \n", repr(data)
  echo data.e[0]

mA(a)
mB(b)

type
  Foo[N: static[int], Z: static[string]] = object

macro staticIntMacro(f: static[int]): untyped = echo f
staticIntMacro 10

var
  x: Foo[20, "Test"]

macro genericMacro[N; Z: static[string]](f: Foo[N, Z], ll = 3, zz = 12): untyped =
  echo N, Z

genericMacro x

template genericTemplate[N, Z](f: Foo[N, Z], ll = 3, zz = 12): int = N

static:
  echo genericTemplate(x)