tstaticparamsmacro.nim 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. discard """
  2. nimout: '''
  3. letters
  4. aa
  5. bb
  6. numbers
  7. 11
  8. 22
  9. AST a
  10. @[(c: 11, d: 22), (c: 33, d: 44)]
  11. AST b
  12. (e: @[55, 66], f: @[77, 88])
  13. 55
  14. 10
  15. 20Test
  16. 20
  17. '''
  18. """
  19. import macros
  20. type
  21. TConfig = tuple
  22. letters: seq[string]
  23. numbers:seq[int]
  24. const data: Tconfig = (@["aa", "bb"], @[11, 22])
  25. macro mymacro(data: static[TConfig]): untyped =
  26. echo "letters"
  27. for s in items(data.letters):
  28. echo s
  29. echo "numbers"
  30. for n in items(data.numbers):
  31. echo n
  32. mymacro(data)
  33. type
  34. Ta = seq[tuple[c:int, d:int]]
  35. Tb = tuple[e:seq[int], f:seq[int]]
  36. const
  37. a : Ta = @[(11, 22), (33, 44)]
  38. b : Tb = (@[55,66], @[77, 88])
  39. macro mA(data: static[Ta]): untyped =
  40. echo "AST a\n", repr(data)
  41. macro mB(data: static[Tb]): untyped =
  42. echo "AST b\n", repr(data)
  43. echo data.e[0]
  44. mA(a)
  45. mB(b)
  46. type
  47. Foo[N: static[int], Z: static[string]] = object
  48. macro staticIntMacro(f: static[int]): untyped = echo f
  49. staticIntMacro 10
  50. var
  51. x: Foo[20, "Test"]
  52. macro genericMacro[N; Z: static[string]](f: Foo[N, Z], ll = 3, zz = 12): untyped =
  53. echo N, Z
  54. genericMacro x
  55. template genericTemplate[N, Z](f: Foo[N, Z], ll = 3, zz = 12): int = N
  56. static:
  57. echo genericTemplate(x)