tstaticparams.nim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. discard """
  2. output: "abracadabra\ntest\n3\n15\n4\n2\nfloat\n3\nfloat\nyin\nyang\n2\n4\n4\n2\n3"
  3. """
  4. type
  5. TFoo[T; Val: static[string]] = object
  6. data: array[4, T]
  7. TBar[T; I: static[int]] = object
  8. data: array[I, T]
  9. TA1[T; I: static[int]] = array[I, T]
  10. TA2[T; I: static[int]] = array[0..I, T]
  11. TA3[T; I: static[int]] = array[I-1, T]
  12. TObj = object
  13. x: TA3[int, 3]
  14. proc takeFoo(x: TFoo) =
  15. echo "abracadabra"
  16. echo TFoo.Val
  17. var x: TFoo[int, "test"]
  18. takeFoo(x)
  19. var y: TBar[float, 4]
  20. echo high(y.data)
  21. var
  22. t1: TA1[float, 1]
  23. t2: TA2[string, 4]
  24. t3: TA3[int, 10]
  25. t4: TObj
  26. # example from the manual:
  27. type
  28. Matrix[M,N: static[int]; T] = array[0..(M*N - 1), T]
  29. # Note how `Number` is just a type constraint here, while
  30. # `static[int]` requires us to supply a compile-time int value
  31. AffineTransform2D[T] = Matrix[3, 3, T]
  32. AffineTransform3D[T] = Matrix[4, 4, T]
  33. var m: AffineTransform3D[float]
  34. echo high(m)
  35. proc getRows(mtx: Matrix): int =
  36. result = mtx.M
  37. echo getRows(m)
  38. # issue 997
  39. type TTest[T: static[int], U: static[int]] = array[0..T*U, int]
  40. type TTestSub[N: static[int]] = TTest[1, N]
  41. var z: TTestSub[2]
  42. echo z.high
  43. # issue 1049
  44. proc matrix_1*[M, N, T](mat: Matrix[M,N,T], a: array[N, int]) = discard
  45. proc matrix_2*[M, N, T](mat: Matrix[M,N,T], a: array[N+1, int]) = discard
  46. proc matrix_3*[M, N: static[int]; T](mat: Matrix[M,N,T], a: array[N, int]) = discard
  47. proc matrix_4*[M, N: static[int]; T](mat: Matrix[M,N,T], a: array[N+1, int]) = discard
  48. var
  49. tmat: Matrix[4,4,int]
  50. ar1: array[4, int]
  51. ar2: array[5, int]
  52. matrix_1(tmat, ar1)
  53. matrix_2(tmat, ar2)
  54. matrix_3(tmat, ar1)
  55. matrix_4(tmat, ar2)
  56. template reject(x): untyped =
  57. static: assert(not compiles(x))
  58. # test with arrays of wrong size
  59. reject matrix_1(tmat, ar2)
  60. reject matrix_2(tmat, ar1)
  61. reject matrix_3(tmat, ar2)
  62. reject matrix_4(tmat, ar1)
  63. # bug 1820
  64. type
  65. T1820_1[T; Y: static[int]] = object
  66. bar: T
  67. proc intOrFloat*[Y](f: T1820_1[int, Y]) = echo "int"
  68. proc intOrFloat*[Y](f: T1820_1[float, Y]) = echo "float"
  69. proc threeOrFour*[T](f: T1820_1[T, 3]) = echo "3"
  70. proc threeOrFour*[T](f: T1820_1[T, 4]) = echo "4"
  71. var foo_1: T1820_1[float, 3]
  72. foo_1.intOrFloat
  73. foo_1.threeOrFour
  74. type
  75. YinAndYang = enum
  76. Yin,
  77. Yang
  78. T1820_2[T; Y: static[YinAndYang]] = object
  79. bar: T
  80. proc intOrFloat*[Y](f: T1820_2[int, Y]) = echo "int"
  81. proc intOrFloat*[Y](f: T1820_2[float, Y]) = echo "float"
  82. proc yinOrYang*[T](f: T1820_2[T, YinAndYang.Yin]) = echo "yin"
  83. proc yinOrYang*[T](f: T1820_2[T, Yang]) = echo "yang"
  84. var foo_2: T1820_2[float, Yin]
  85. var foo_3: T1820_2[float, YinAndYang.Yang]
  86. foo_2.intOrFloat
  87. foo_2.yinOrYang
  88. foo_3.yinOrYang
  89. # bug 1859
  90. type
  91. TypeWith2Params[N, M: static[int]] = object
  92. proc bindBothParams[N](x: TypeWith2Params[N, N]) = discard
  93. proc dontBind1[N,M](x: TypeWith2Params[N, M]) = discard
  94. proc dontBind2(x: TypeWith2Params) = discard
  95. var bb_1: TypeWith2Params[2, 2]
  96. var bb_2: TypeWith2Params[2, 3]
  97. bindBothParams(bb_1)
  98. reject bindBothParams(bb_2)
  99. dontBind1 bb_1
  100. dontBind1 bb_2
  101. dontBind2 bb_1
  102. dontBind2 bb_2
  103. # https://github.com/nim-lang/Nim/issues/4524
  104. const
  105. size* = 2
  106. proc arraySize[N: static[int]](A: array[N, int]): int =
  107. result = A.high - A.low + 1
  108. var A: array[size, int] = [1, 2]
  109. echo arraySize(A)
  110. # https://github.com/nim-lang/Nim/issues/3153
  111. proc outSize1[M: static[int], A](xs: array[M, A]): int = M
  112. echo outSize1([1, 2, 3, 4])
  113. type
  114. Arr[N: static[int], A] = array[N, A]
  115. proc outSize2[M: static[int], A](xs: Arr[M, A]): int = M
  116. echo outSize2([1, 2, 3, 4]) # 4
  117. echo outSize2([
  118. [1, 2, 3],
  119. [4, 5, 6]
  120. ]) # 2
  121. proc inSize[M, N: static[int]](xs: Arr[M, Arr[N, int]]): int = N
  122. echo inSize([
  123. [1, 2, 3],
  124. [4, 5, 6]
  125. ])
  126. block: # #12864
  127. template fun() =
  128. type Object = object
  129. proc fun(f: Object): int = 1
  130. proc fun(f: static[int]): int = 2
  131. doAssert fun(Object()) == 1
  132. var a: Object
  133. doAssert fun(a) == 1
  134. proc fun2(f: Object): int = 1
  135. proc fun2(f: static[Object]): int = 2
  136. doAssert fun2(Object()) == 2
  137. doAssert fun2(a) == 1
  138. const a2 = Object()
  139. doAssert fun2(a2) == 2
  140. fun()
  141. static: fun()
  142. when true: #12864 original snippet
  143. import times
  144. discard times.format(initDateTime(30, mMar, 2017, 0, 0, 0, 0, utc()), TimeFormat())