tsecondarrayproperty.nim 602 B

1234567891011121314151617181920212223242526272829303132
  1. discard """
  2. output: "4"
  3. """
  4. type
  5. TFoo = object
  6. data: array[0..100, int]
  7. TSecond = distinct TFoo
  8. proc `[]` (self: var TFoo, x: int): var int =
  9. return self.data[x]
  10. proc `[]=` (self: var TFoo, x, y: int) =
  11. # only `[]` returning a 'var T' seems to not work for now :-/
  12. self.data[x] = y
  13. proc second(self: var TFoo): var TSecond =
  14. return TSecond(self)
  15. proc `[]`(self: var TSecond, x: int): var int =
  16. return TFoo(self).data[2*x]
  17. var f: TFoo
  18. for i in 0..f.data.high: f[i] = 2 * i
  19. echo f.second[1]
  20. #echo `second[]`(f,1)
  21. # this is the only way I could use it, but not what I expected