tgettype2.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. discard """
  2. output: '''
  3. ############
  4. #### gt ####
  5. ############
  6. gt(Foo): typeDesc[Foo]
  7. gt(Bar): typeDesc[Bar]
  8. gt(Baz): typeDesc[int]
  9. gt(foo): distinct[int]
  10. gt(bar): distinct[int]
  11. gt(baz): int, int
  12. gt(v): seq[int]
  13. gt(vv): seq[float]
  14. gt(t): distinct[tuple[int, int]]
  15. gt(tt): distinct[tuple[float, float]]
  16. gt(s): distinct[tuple[int, int]]
  17. #############
  18. #### gt2 ####
  19. #############
  20. gt2(Foo): Foo
  21. gt2(Bar): Bar
  22. gt2(Baz): Baz
  23. gt2(foo): Foo
  24. gt2(bar): Bar
  25. gt2(baz): Baz
  26. gt2(v): seq[int]
  27. gt2(vv): seq[float]
  28. gt2(t): MyType[system.int]
  29. gt2(tt): MyType[system.float]
  30. gt2(s): MySimpleType
  31. '''
  32. """
  33. import macros, typetraits
  34. type Foo = distinct int
  35. type Bar = distinct int
  36. type Baz = int
  37. let foo = 0.Foo
  38. let bar = 1.Bar
  39. let baz = 2.Baz
  40. type MyType[T] = distinct tuple[a,b:T]
  41. type MySimpleType = distinct tuple[a,b: int]
  42. var v: seq[int]
  43. var vv: seq[float]
  44. var t: MyType[int]
  45. var tt: MyType[float]
  46. var s: MySimpleType
  47. echo "############"
  48. echo "#### gt ####"
  49. echo "############"
  50. macro gt(a: typed): string =
  51. let b = a.getType
  52. var str = "gt(" & $a & "):\t" & b.repr
  53. if b.kind == nnkSym: # bad predicat to check weather the type has an implementation
  54. str = str & ", " & b.getType.repr # append the implementation to the result
  55. result = newLit(str)
  56. echo gt(Foo) # typeDesc[Foo]
  57. echo gt(Bar) # typeDesc[Bar]
  58. echo gt(Baz) # typeDesc[int] shouldn't it be typeDesc[Baz]?
  59. echo gt(foo) # distinct[int] I would prefer Foo, distinct[int]
  60. echo gt(bar) # distinct[int] I would prefer Bar, distinct[int]
  61. echo gt(baz) # int, int I would prefer Baz, int
  62. echo gt(v) # seq[int], ok
  63. echo gt(vv) # seq[float], ok
  64. echo gt(t) # MyType, distinct[tuple[int, int]] I would prefer MyType[int], distinct[tuple[int, int]]
  65. echo gt(tt) # MyType, distinct[tuple[float, float]] I would prefer MyType[float], distinct[tuple[int, int]]
  66. echo gt(s) # distinct[tuple[int, int]] I would prefer MySimpleType, distinct[tuple[int,int]]
  67. echo "#############"
  68. echo "#### gt2 ####"
  69. echo "#############"
  70. # get type name via typetraits
  71. macro gt2(a: typed): string =
  72. let prefix = "gt2(" & $a & "): \t"
  73. result = quote do:
  74. `prefix` & `a`.type.name
  75. echo gt2(Foo) # Foo shouldn't this be typeDesc[Foo] ?
  76. echo gt2(Bar) # Bar shouldn't this be typeDesc[Bar] ?
  77. echo gt2(Baz) # Baz shouldn't this be typeDesc[Baz] ?
  78. echo gt2(foo) # Foo
  79. echo gt2(bar) # Bar
  80. echo gt2(baz) # Baz
  81. echo gt2(v) # seq[int]
  82. echo gt2(vv) # seq[float]
  83. echo gt2(t) # MyType[system.int] why is it system.int and not just int like in seq?
  84. echo gt2(tt) # MyType[system.float] why is it system.float and not just float like in seq?
  85. echo gt2(s) # MySimpleType