tmacrogenerics.nim 683 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. discard """
  2. file: "tmacrogenerics.nim"
  3. msg: '''
  4. instantiation 1 with typedesc and typedesc
  5. counter: 1
  6. '''
  7. output: "int\nfloat\nint\nstring"
  8. """
  9. import typetraits, macros
  10. var counter {.compileTime.} = 0
  11. macro makeBar(A, B: typedesc): typedesc =
  12. inc counter
  13. echo "instantiation ", counter, " with ", A.name, " and ", B.name
  14. result = A
  15. type
  16. Bar[T, U] = makeBar(T, U)
  17. var bb1: Bar[int, float]
  18. var bb2: Bar[float, string]
  19. var bb3: Bar[int, float]
  20. var bb4: Bar[string, string]
  21. proc match(a: int) = echo "int"
  22. proc match(a: string) = echo "string"
  23. proc match(a: float) = echo "float"
  24. match(bb1)
  25. match(bb2)
  26. match(bb3)
  27. match(bb4)
  28. static:
  29. echo "counter: ", counter