tmetatype_issues.nim 2.6 KB

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