tuple_canon.nim 2.8 KB

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