twrong_field_caching.nim 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. discard """
  2. output: '''a23: 2x3
  3. a32: 3x2
  4. transpose A
  5. t32: 3x2
  6. transpose B
  7. x23: 2x3 (2x3)
  8. x32: 3x2 (3x2)'''
  9. """
  10. # bug #2125
  11. # Suppose we have the following type for a rectangular array:
  12. type
  13. RectArray*[R, C: static[int], T] = distinct array[R * C, T]
  14. var a23: RectArray[2, 3, int]
  15. var a32: RectArray[3, 2, int]
  16. echo "a23: ", a23.R, "x", a23.C
  17. echo "a32: ", a32.R, "x", a32.C
  18. # Output:
  19. # a23: 2x3
  20. # a32: 3x2
  21. # Looking good. Let's add a proc:
  22. proc transpose*[R, C, T](m: RectArray[R, C, T]): RectArray[C, R, T] =
  23. echo "transpose A"
  24. var t32 = a23.transpose
  25. echo "t32: ", t32.R, "x", t32.C
  26. # Output:
  27. # t32: 3x2
  28. # Everything is still OK. Now let's use the rectangular array inside another
  29. # generic type:
  30. type
  31. Matrix*[R, C: static[int], T] = object
  32. theArray*: RectArray[R, C, T]
  33. #var m23: Matrix[2, 3, int]
  34. #var m32: Matrix[3, 2, int]
  35. #echo "m23: ", m23.R, "x", m23.C, " (", m23.theArray.R, "x", m23.theArray.C, ")"
  36. #echo "m32: ", m32.R, "x", m32.C, " (", m32.theArray.R, "x", m32.theArray.C, ")"
  37. # Output:
  38. # m23: 2x3 (2x3)
  39. # m32: 3x2 (3x2)
  40. # Everything is still as expected. Now let's add the following proc:
  41. proc transpose*[R, C, T](m: Matrix[R, C, T]): Matrix[C, R, T] =
  42. echo "transpose B"
  43. var x23: Matrix[2, 3, int]
  44. var x32 = x23.transpose
  45. echo "x23: ", x23.R, "x", x23.C, " (", x23.theArray.R, "x", x23.theArray.C, ")"
  46. echo "x32: ", x32.R, "x", x32.C, " (", x32.theArray.R, "x", x32.theArray.C, ")"
  47. # Output:
  48. # x23: 2x3 (2x3)
  49. # x32: 3x2 (3x2) <--- this is incorrect. R and C do not match!