tbyvar.nim 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. discard """
  2. output: '''foo 12
  3. bar 12
  4. 2
  5. foo 12
  6. bar 12
  7. 2
  8. 12.5
  9. (nums: @[5, 5, 10, 5, 5, 5, 5, 5, 5, 5])
  10. (nums: @[5, 5, 50, 5, 5, 5, 5, 5, 5, 5])
  11. (nums: @[5, 5, 45, 5, 5, 5, 5, 5, 5, 5])
  12. (nums: @[5, 5, 9, 5, 5, 5, 5, 5, 5, 5])
  13. asd
  14. '''
  15. """
  16. # bug #1489
  17. proc foo(x: int) = echo "foo ", x
  18. proc bar(y: var int) = echo "bar ", y
  19. var x = 12
  20. foo(x)
  21. bar(x)
  22. # bug #1490
  23. var y = 1
  24. y *= 2
  25. echo y
  26. proc main =
  27. var x = 12
  28. foo(x)
  29. bar(x)
  30. var y = 1
  31. y *= 2
  32. echo y
  33. main()
  34. # Test: pass var seq to var openarray
  35. var s = @[2, 1]
  36. proc foo(a: var openarray[int]) = a[0] = 123
  37. proc bar(s: var seq[int], a: int) =
  38. doAssert(a == 5)
  39. foo(s)
  40. s.bar(5)
  41. doAssert(s == @[123, 1])
  42. import tables
  43. block: # Test get addr of byvar return value
  44. var t = initTable[string, int]()
  45. t["hi"] = 5
  46. let a = addr t["hi"]
  47. a[] = 10
  48. doAssert(t["hi"] == 10)
  49. block: # Test var arg inside case expression. #5244
  50. proc foo(a: var string) =
  51. a = case a
  52. of "a": "error"
  53. of "b": "error"
  54. else: a
  55. var a = "ok"
  56. foo(a)
  57. doAssert(a == "ok")
  58. proc mainowar =
  59. var x = 9.0
  60. x += 3.5
  61. echo x
  62. mainowar()
  63. # bug #5608
  64. type Foo = object
  65. nums : seq[float]
  66. proc newFoo(len : int, default = 0.0) : Foo =
  67. result = Foo()
  68. result.nums = newSeq[float](len)
  69. for i in 0..(len - 1):
  70. result.nums[i] = default
  71. proc `[]=`(f : var Foo, i : int, v : float) =
  72. f.nums[i] = v
  73. proc `[]`(f : Foo, i : int) : float = f.nums[i]
  74. proc `[]`(f : var Foo, i : int) : var float = f.nums[i]
  75. var f = newFoo(10,5)
  76. f[2] += 5
  77. echo f
  78. f[2] *= 5
  79. echo f
  80. f[2] -= 5
  81. echo f
  82. f[2] /= 5
  83. echo f
  84. # regression for #5608
  85. import tables
  86. type
  87. SomeObj = ref object
  88. s: cstring
  89. var a = initTable[cstring, Table[cstring, SomeObj]]()
  90. var b = initTable[cstring, SomeObj]()
  91. b.add(cstring"b", SomeObj(s: cstring"asd"))
  92. a.add(cstring"a", b)
  93. echo a[cstring"a"][cstring"b"].s