ttuples_various.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. discard """
  2. output: '''
  3. it's nil
  4. @[1, 2, 3]
  5. '''
  6. """
  7. import macros
  8. block anontuples:
  9. proc `^` (a, b: int): int =
  10. result = 1
  11. for i in 1..b: result = result * a
  12. var m = (0, 5)
  13. var n = (56, 3)
  14. m = (n[0] + m[1], m[1] ^ n[1])
  15. doAssert m == (61, 125)
  16. # also test we can produce unary anon tuples in a macro:
  17. macro mm(): untyped =
  18. result = newTree(nnkTupleConstr, newLit(13))
  19. proc nowTuple(): (int,) =
  20. result = (0,)
  21. doAssert nowTuple() == (Field0: 0)
  22. doAssert mm() == (Field0: 13)
  23. block unpack_asgn:
  24. proc foobar(): (int, int) = (2, 4)
  25. # test within a proc:
  26. proc pp(x: var int) =
  27. var y: int
  28. (y, x) = foobar()
  29. template pt(x) =
  30. var y: int
  31. (x, y) = foobar()
  32. # test within a generic:
  33. proc pg[T](x, y: var T) =
  34. pt(x)
  35. # test as a top level statement:
  36. var x, y, a, b: int
  37. # test for regression:
  38. (x, y) = (1, 2)
  39. (x, y) = fooBar()
  40. doAssert x == 2
  41. doAssert y == 4
  42. pp(a)
  43. doAssert a == 4
  44. pg(a, b)
  45. doAssert a == 2
  46. doAssert b == 0
  47. block unpack_const:
  48. const (a, ) = (1, )
  49. doAssert a == 1
  50. const (b, c) = (2, 3)
  51. doAssert b == 2
  52. doAssert c == 3
  53. # bug #10098
  54. const (x, y, z) = (4, 5, 6)
  55. doAssert x == 4
  56. doAssert y == 5
  57. doAssert z == 6
  58. block tuple_subscript:
  59. proc`[]` (t: tuple, key: string): string =
  60. for name, field in fieldPairs(t):
  61. if name == key:
  62. return $field
  63. return ""
  64. proc`[]` [A,B](t: tuple, key: string, op: (proc(x: A): B)): B =
  65. for name, field in fieldPairs(t):
  66. when field is A:
  67. if name == key:
  68. return op(field)
  69. proc`[]=`[T](t: var tuple, key: string, val: T) =
  70. for name, field in fieldPairs(t):
  71. when field is T:
  72. if name == key:
  73. field = val
  74. var tt = (a: 1, b: "str1")
  75. # test built in operator
  76. tt[0] = 5
  77. doAssert tt[0] == 5
  78. doAssert `[]`(tt, 0) == 5
  79. # test overloaded operator
  80. tt["b"] = "str2"
  81. doAssert tt["b"] == "str2"
  82. doAssert `[]`(tt, "b") == "str2"
  83. doAssert tt["b", proc(s: string): int = s.len] == 4
  84. block tuple_with_seq:
  85. template foo(s: string = "") =
  86. if s.len == 0:
  87. echo "it's nil"
  88. else:
  89. echo s
  90. foo
  91. # bug #2632
  92. proc takeTup(x: tuple[s: string;x: seq[int]]) =
  93. discard
  94. takeTup(("foo", @[]))
  95. #proc foobar(): () =
  96. proc f(xs: seq[int]) =
  97. discard
  98. proc g(t: tuple[n:int, xs:seq[int]]) =
  99. discard
  100. when true:
  101. f(@[]) # OK
  102. g((1,@[1])) # OK
  103. g((0,@[])) # NG
  104. # bug #2630
  105. type T = tuple[a: seq[int], b: int]
  106. var t: T = (@[1,2,3], 7)
  107. proc test(s: seq[int]): T =
  108. echo s
  109. (s, 7)
  110. t = test(t.a)