treentranttypes.nim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. discard """
  2. output: '''
  3. (10, ("test", 1.2))
  4. 3x3 Matrix [[0.0, 2.0, 3.0], [2.0, 0.0, 5.0], [2.0, 0.0, 5.0]]
  5. 2x3 Matrix [[0.0, 2.0, 3.0], [2.0, 0.0, 5.0]]
  6. 2x3 Literal [[0.0, 2.0, 3.0], [2.0, 0.0, 5.0]]
  7. 2x3 Matrix [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
  8. 2x2 ArrayArray[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
  9. 2x3 ArrayVector[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
  10. 2x3 VectorVector [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
  11. 2x3 VectorArray [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
  12. @[1, 2]
  13. @[1, 2]
  14. @[1, 2]@[3, 4]
  15. @[1, 2]@[3, 4]
  16. '''
  17. """
  18. # https://github.com/nim-lang/Nim/issues/5962
  19. type
  20. ArrayLike[A, B] = (A, B)
  21. VectorLike*[SIZE, T] = ArrayLike[SIZE, T]
  22. MatrixLike*[M, N, T] = VectorLike[M, VectorLike[N, T]]
  23. proc tupleTest =
  24. let m: MatrixLike[int, string, float] = (10, ("test", 1.2))
  25. echo m
  26. tupleTest()
  27. type
  28. Vector*[K: static[int], T] =
  29. array[K, T]
  30. Matrix*[M: static[int]; N: static[int]; T] =
  31. Vector[M, Vector[N, T]]
  32. proc arrayTest =
  33. # every kind of square matrix works just fine
  34. let mat_good: Matrix[3, 3, float] = [[0.0, 2.0, 3.0],
  35. [2.0, 0.0, 5.0],
  36. [2.0, 0.0, 5.0]]
  37. echo "3x3 Matrix ", repr(mat_good)
  38. # this does not work with explicit type signature (the matrix seems to always think it is NxN instead)
  39. let mat_fail: Matrix[2, 3, float] = [[0.0, 2.0, 3.0],
  40. [2.0, 0.0, 5.0]]
  41. echo "2x3 Matrix ", repr(mat_fail)
  42. # this literal seems to work just fine
  43. let mat_also_good = [[0.0, 2.0, 3.0],
  44. [2.0, 0.0, 5.0]]
  45. echo "2x3 Literal ", repr(mat_also_good)
  46. # but making a named type out of this leads to pretty nasty runtime behavior
  47. var mat_fail_runtime: Matrix[2, 3, float]
  48. echo "2x3 Matrix ", repr(mat_fail_runtime)
  49. # cutting out the matrix type middle man seems to solve our problem
  50. var mat_ok_runtime: array[2, array[3, float]]
  51. echo "2x2 ArrayArray", repr(mat_ok_runtime)
  52. # this is fine too
  53. var mat_ok_runtime_2: array[2, Vector[3, float]]
  54. echo "2x3 ArrayVector", repr(mat_ok_runtime_2)
  55. # here we are in trouble again
  56. var mat_fail_runtime_2: Vector[2, Vector[3, float]]
  57. echo "2x3 VectorVector ", repr(mat_fail_runtime_2)
  58. # and here we are fine again
  59. var mat_ok_runtime_3: Vector[2, array[3, float]]
  60. echo "2x3 VectorArray ", repr(mat_ok_runtime_3)
  61. arrayTest()
  62. # https://github.com/nim-lang/Nim/issues/5756
  63. type
  64. Vec*[N : static[int]] = object
  65. arr*: array[N, int32]
  66. Mat*[M,N: static[int]] = object
  67. arr*: array[M, Vec[N]]
  68. proc vec2*(x,y:int32) : Vec[2] =
  69. result.arr = [x,y]
  70. proc mat2*(a,b: Vec[2]): Mat[2,2] =
  71. result.arr = [a,b]
  72. const a = vec2(1,2)
  73. echo @(a.arr)
  74. let x = a
  75. echo @(x.arr)
  76. const b = mat2(vec2(1, 2), vec2(3, 4))
  77. echo @(b.arr[0].arr), @(b.arr[1].arr)
  78. let y = b
  79. echo @(y.arr[0].arr), @(y.arr[1].arr)
  80. import macros
  81. block: # issue #5121
  82. type
  83. A = object
  84. AConst[X] = A
  85. macro dumpType(t: typedesc): untyped =
  86. result = newTree(nnkTupleConstr, newLit $t.getType[1].typeKind, newLit t.getType[1].treeRepr)
  87. doAssert dumpType(A) == ("ntyObject", "Sym \"A\"")