tbyvar.nim 1.9 KB

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