tstaticparammacro.nim 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. discard """
  2. nimout: '''letters
  3. aa
  4. bb
  5. numbers
  6. 11
  7. 22
  8. AST a
  9. [(11, 22), (33, 44)]
  10. AST b
  11. ([55, 66], [77, 88])
  12. 55
  13. 10
  14. 20Test
  15. 20
  16. '''
  17. """
  18. import macros
  19. type
  20. TConfig = tuple
  21. letters: seq[string]
  22. numbers:seq[int]
  23. const data: Tconfig = (@["aa", "bb"], @[11, 22])
  24. macro mymacro(data: static[TConfig]): untyped =
  25. echo "letters"
  26. for s in items(data.letters):
  27. echo s
  28. echo "numbers"
  29. for n in items(data.numbers):
  30. echo n
  31. mymacro(data)
  32. type
  33. Ta = seq[tuple[c:int, d:int]]
  34. Tb = tuple[e:seq[int], f:seq[int]]
  35. const
  36. a : Ta = @[(11, 22), (33, 44)]
  37. b : Tb = (@[55,66], @[77, 88])
  38. macro mA(data: static[Ta]): untyped =
  39. echo "AST a\n", repr(data)
  40. macro mB(data: static[Tb]): untyped =
  41. echo "AST b\n", repr(data)
  42. echo data.e[0]
  43. mA(a)
  44. mB(b)
  45. type
  46. Foo[N: static[int], Z: static[string]] = object
  47. macro staticIntMacro(f: static[int]): untyped =
  48. 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) # Error: internal error: (filename: compiler/evaltempl.nim, line: 39)