tmetatype_issues.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. discard """
  2. output:'''
  3. void
  4. ("string", "string")
  5. 1 mod 7
  6. @[2, 2, 2, 2, 2]
  7. impl 2 called
  8. asd
  9. Foo
  10. Bar
  11. '''
  12. joinable: false
  13. """
  14. import typetraits, macros
  15. block t898:
  16. proc measureTime(e: auto) =
  17. echo e.type.name
  18. proc generate(a: int): void =
  19. discard
  20. proc runExample =
  21. var builder: int = 0
  22. measureTime:
  23. builder.generate()
  24. measureTime:
  25. discard
  26. block t7528:
  27. macro bar(n: untyped) =
  28. result = newNimNode(nnkStmtList, n)
  29. result.add(newCall("write", newIdentNode("stdout"), n))
  30. proc foo0[T](): auto = return (T.name, T.name)
  31. bar foo0[string]()
  32. echo ""
  33. block t5638:
  34. type X = object
  35. a_impl: int
  36. proc a(x: X): int =
  37. x.a_impl
  38. var x: X
  39. assert(not compiles((block:
  40. x.a = 1
  41. )))
  42. block t3706:
  43. type Modulo[M: static[int]] = distinct int
  44. proc modulo(a: int, M: static[int]): Modulo[M] = Modulo[M](a %% M)
  45. proc `+`[M: static[int]](a, b: Modulo[M]): Modulo[M] = (a.int + b.int).modulo(M)
  46. proc `$`[M: static[int]](a: Modulo[M]): string = $(a.int) & " mod " & $(M)
  47. let
  48. a = 3.modulo(7)
  49. b = 5.modulo(7)
  50. echo a + b
  51. block t3144:
  52. type IntArray[N: static[int]] = array[N, int]
  53. proc `$`(a: IntArray): string = $(@(a))
  54. proc `+=`[N: static[int]](a: var IntArray[N], b: IntArray[N]) =
  55. for i in 0 ..< N:
  56. a[i] += b[i]
  57. proc zeros(N: static[int]): IntArray[N] =
  58. for i in 0 ..< N:
  59. result[i] = 0
  60. proc ones(N: static[int]): IntArray[N] =
  61. for i in 0 ..< N:
  62. result[i] = 1
  63. proc sum[N: static[int]](vs: seq[IntArray[N]]): IntArray[N] =
  64. result = zeros(N)
  65. for v in vs:
  66. result += v
  67. echo sum(@[ones(5), ones(5)])
  68. block t6533:
  69. type Value[T: static[int]] = typedesc
  70. proc foo(order: Value[1]): auto = 0
  71. doAssert foo(Value[1]) == 0
  72. block t2266:
  73. proc impl(op: static[int]) = echo "impl 1 called"
  74. proc impl(op: static[int], init: int) = echo "impl 2 called"
  75. macro wrapper2: untyped = newCall(bindSym"impl", newLit(0), newLit(0))
  76. wrapper2() # Code generation for this fails.
  77. block t602:
  78. type
  79. TTest = object
  80. TTest2 = object
  81. TFoo = TTest | TTest2
  82. proc f(src: ptr TFoo, dst: ptr TFoo) =
  83. echo("asd")
  84. var x: TTest
  85. f(addr x, addr x)
  86. block t3338:
  87. type
  88. Base[T] = Foo[T] | Bar[T]
  89. Foo[T] = ref object
  90. x: T
  91. Bar[T] = ref object
  92. x: T
  93. proc test[T](ks: Foo[T], x, y: T): T =
  94. echo("Foo")
  95. return x + y + ks.x
  96. proc test[T](ks: Bar[T], x, y: T): T =
  97. echo("Bar")
  98. return x
  99. proc add[T](ksa: Base[T]) =
  100. var test = ksa.test(5, 10)
  101. ksa.x = test
  102. var t1 = Foo[int32]()
  103. t1.add()
  104. doAssert t1.x == 15
  105. var t2 = Bar[int32]()
  106. t2.add()
  107. doAssert t2.x == 5