tviews2.nim 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. discard """
  2. targets: "c js"
  3. """
  4. {.experimental: "views".}
  5. block:
  6. type
  7. Foo = object
  8. id: openArray[char]
  9. proc foo(): Foo =
  10. var source = "1245"
  11. result = Foo(id: source.toOpenArray(0, 1))
  12. doAssert foo().id == @['1', '2']
  13. block: # bug #15778
  14. type
  15. Reader = object
  16. data: openArray[char]
  17. current: int
  18. var count = 0
  19. proc read(data: var Reader, length: int): openArray[char] =
  20. inc count
  21. let start = data.current
  22. data.current.inc length
  23. return data.data.toOpenArray(start, data.current-1)
  24. var data = "hello there"
  25. var reader = Reader(data: data.toOpenArray(0, data.len-1), current: 0)
  26. doAssert @(reader.read(2)) == @['h', 'e']
  27. doAssert @(reader.read(3)) == @['l', 'l', 'o']
  28. doAssert count == 2
  29. block: # bug #16671
  30. block:
  31. type X = ref object of RootObj
  32. type Y = ref object of X
  33. field: openArray[int]
  34. var s: seq[X]
  35. proc f() =
  36. s.add(Y(field: [1]))
  37. f()
  38. block:
  39. type X = ref object of RootObj
  40. type Y = ref object of X
  41. field: openArray[int]
  42. var s: seq[X]
  43. proc f() =
  44. s.add(Y(field: toOpenArray([1, 2, 3], 0, 1)))
  45. f()
  46. block:
  47. proc foo(x: openArray[char]) =
  48. discard x
  49. foo("12254")
  50. foo(@['a', 'b'])
  51. var a1 = "12254"
  52. foo(a1)
  53. var a2 = @['a', 'b']
  54. foo(a2)
  55. var s = "138443"
  56. var ooo: openArray[char] = s
  57. var xxx: openArray[char] = ooo
  58. foo(ooo)
  59. foo(xxx)