tmacrogenerics.nim 810 B

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