tconst_views.nim 700 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. discard """
  2. cmd: "nim c --experimental:views $file"
  3. output: '''(data: [1, 2, 3], other: 4)
  4. [1, 20, 3]'''
  5. """
  6. type
  7. Foo = object
  8. data: openArray[int]
  9. other: int
  10. const
  11. c = Foo(data: [1, 2, 3], other: 4)
  12. c2 = Foo(data: [1, 20, 3], other: 4)
  13. proc `$`(x: openArray[int]): string =
  14. result = "["
  15. for i in x:
  16. if result.len > 1: result.add ", "
  17. result.add $i
  18. result.add "]"
  19. echo c
  20. echo c2.data
  21. type MyObj = object
  22. data: openarray[char]
  23. const
  24. val1 = Foo(data: toOpenArray([1, 2, 3], 1, 1))
  25. val2 = Foo(data: toOpenArray([1, 2, 3], 0, 2))
  26. val3 = MyObj(data: "Hello".toOpenArray(0, 2))
  27. assert val1.data == [2]
  28. assert val2.data == [1, 2, 3]
  29. assert val3.data == "Hel"