tviews1.nim 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. discard """
  2. output: '''11
  3. 22
  4. 33
  5. 3
  6. 2
  7. 3
  8. 3
  9. 15
  10. (oa: [1, 3, 4])'''
  11. targets: "c cpp"
  12. """
  13. {.experimental: "views".}
  14. proc take(a: openArray[int]) =
  15. echo a.len
  16. proc main(s: seq[int]) =
  17. var x: openArray[int] = s
  18. for i in 0 .. high(x):
  19. echo x[i]
  20. take(x)
  21. take(x.toOpenArray(0, 1))
  22. let y = x
  23. take y
  24. take x
  25. main(@[11, 22, 33])
  26. var x: int
  27. proc foo(x: var int): var int =
  28. once: x = 42
  29. return x
  30. var y: var int = foo(x)
  31. y = 15
  32. echo foo(x)
  33. # bug #16132
  34. # bug #18690
  35. type
  36. F = object
  37. oa: openArray[int]
  38. let s1 = @[1,3,4,5,6]
  39. var test = F(oa: toOpenArray(s1, 0, 2))
  40. echo test
  41. type
  42. Foo = object
  43. x: string
  44. y: seq[int]
  45. data: array[10000, byte]
  46. View[T] = object
  47. x: lent T
  48. proc mainB =
  49. let f = Foo(y: @[1, 2, 3])
  50. let foo = View[Foo](x: f)
  51. assert foo.x.x == ""
  52. assert foo.x.y == @[1, 2, 3]
  53. mainB()
  54. # bug #15897
  55. type Outer = ref object
  56. value: int
  57. type Inner = object
  58. owner: var Outer
  59. var o = Outer(value: 1234)
  60. var v = Inner(owner: o).owner.value
  61. doAssert v == 1234