tgettypeinst7737.nim 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. discard """
  2. nimout: '''
  3. seq[int]
  4. CustomSeq[int]
  5. '''
  6. """
  7. import macros, typetraits, sequtils
  8. block: # issue #7737 original
  9. type
  10. CustomSeq[T] = object
  11. data: seq[T]
  12. proc getSubType(T: NimNode): NimNode =
  13. echo getTypeInst(T).repr
  14. result = getTypeInst(T)[1]
  15. macro typed_helper(x: varargs[typed]): untyped =
  16. let foo = getSubType(x[0])
  17. result = quote do: discard
  18. macro untyped_heavylifting(x: varargs[untyped]): untyped =
  19. var containers = nnkArgList.newTree()
  20. for arg in x:
  21. case arg.kind:
  22. of nnkInfix:
  23. if eqIdent(arg[0], "in"):
  24. containers.add arg[2]
  25. else:
  26. discard
  27. result = quote do:
  28. typed_helper(`containers`)
  29. var a, b, c: seq[int]
  30. untyped_heavylifting z in c, x in a, y in b:
  31. discard
  32. ## The following gives me CustomSeq instead
  33. ## of CustomSeq[int] in getTypeInst
  34. var u, v, w: CustomSeq[int]
  35. untyped_heavylifting z in u, x in v, y in w:
  36. discard
  37. block: # issue #7737 comment
  38. type
  39. CustomSeq[T] = object
  40. data: seq[T]
  41. # when using just one argument, `foo` and `bar` should be exactly
  42. # identical.
  43. macro foo(arg: typed): string =
  44. result = newLit(arg.getTypeInst.repr)
  45. macro bar(args: varargs[typed]): untyped =
  46. result = newTree(nnkBracket)
  47. for arg in args:
  48. result.add newLit(arg.getTypeInst.repr)
  49. var
  50. a: seq[int]
  51. b: CustomSeq[int]
  52. doAssert foo(a) == "seq[int]"
  53. doAssert bar(a) == ["seq[int]"]
  54. doAssert foo(b) == "CustomSeq[int]"
  55. doAssert bar(b) == ["CustomSeq[int]"]