tuple_canon.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. discard """
  2. output: '''
  3. vidx 18
  4. 0,0
  5. '''
  6. """
  7. # bug #4626
  8. var foo: (int, array[1, int]) # Tuple must be of length > 1
  9. let bar = (1, [1])
  10. foo = bar # No error if assigned directly
  11. # bug #2250
  12. import
  13. math, strutils
  14. type
  15. Meters = float
  16. Point2[T] = tuple[x, y: T]
  17. HexState* = enum
  18. hsOn, hsOff
  19. Index = uint16
  20. HexGrid* = object
  21. w, h: int ## Width and height of the hex grid.
  22. radius: Meters ## Radius of circle that circumscribes a hexagon.
  23. grid: seq[HexState] ## Information on what hexes are drawn.
  24. HexVtxIndex = enum
  25. hiA, hiB, hiC, hiD, hiE, hiF
  26. HexCoord* = Point2[int]
  27. const
  28. HexDY = sqrt(1.0 - (0.5 * 0.5)) # dy from center to midpoint of 1-2
  29. HexDX = sqrt(1.0 - (HexDY * HexDY)) # dx from center to midpoint of 1-5 (0.5)
  30. let
  31. hexOffsets : array[HexVtxIndex, Point2[float]] = [
  32. (-1.0, 0.0),
  33. (-HexDX, -HexDY),
  34. (HexDX, -HexDY),
  35. (1.0, 0.0),
  36. (HexDX, HexDY),
  37. (-HexDX, HexDY)]
  38. evenSharingOffsets : array[HexVtxIndex, tuple[hc: HexCoord; idx: HexVtxIndex]] = [
  39. ((0,0), hiA),
  40. ((0,0), hiB),
  41. ((1,-1), hiA),
  42. ((1,0), hiB),
  43. ((1,0), hiA),
  44. ((0,1), hiB)]
  45. oddSharingOffsets : array[HexVtxIndex, tuple[hc: HexCoord; idx: HexVtxIndex]] = [
  46. ((0,0), hiA),
  47. ((0,0), hiB),
  48. ((1,0), hiA),
  49. ((1,1), hiB),
  50. ((1,1), hiA),
  51. ((0,1), hiB)]
  52. template odd*(i: int) : untyped =
  53. (i and 1) != 0
  54. proc vidx(hg: HexGrid; col, row: int; i: HexVtxIndex) : Index =
  55. #NOTE: this variation compiles
  56. #var offset : type(evenSharingOffsets[i])
  57. #
  58. #if odd(col):
  59. # offset = oddSharingOffsets[i]
  60. #else:
  61. # offset = evenSharingOffsets[i]
  62. let
  63. #NOTE: this line generates the bad code
  64. offset = (if odd(col): oddSharingOffsets[i] else: evenSharingOffsets[i])
  65. x = col + 1 + offset.hc.x
  66. y = row + 1 + offset.hc.y
  67. result = Index(x*2 + y * (hg.w + 2)*2 + int(offset.idx))
  68. proc go() =
  69. var hg : HexGrid
  70. echo "vidx ", $vidx(hg, 1, 2, hiC)
  71. go()
  72. # another sighashes problem: In tuples we have to ignore ranges.
  73. type
  74. Position = tuple[x, y: int16]
  75. n16 = range[0'i16..high(int16)]
  76. proc print(pos: Position) =
  77. echo $pos.x, ",", $pos.y
  78. var x = 0.n16
  79. var y = 0.n16
  80. print((x, y))
  81. # bug #6889
  82. proc createProgressSetterWithPropSetter[T](setter: proc(v: T)) = discard
  83. type A = distinct array[4, float32]
  84. type B = distinct array[3, float32]
  85. type Foo[T] = tuple
  86. setter: proc(v: T)
  87. proc getFoo[T](): Foo[T] = discard
  88. createProgressSetterWithPropSetter(getFoo[A]().setter)
  89. createProgressSetterWithPropSetter(getFoo[B]().setter)