Th documentation says that procedures marked with 'lent' return a hidden pointer. But the code below prints two different values which shows that a copy of the data is being returned instead of a pointer (Nim version 2.0.0 used) . I assume that I am misunderstanding how 'lent' works, can anyone clear up my confusion?
type T = object
x:seq[int]
proc create():T =
result.x = newseq[int](3)
proc test( t:T ): lent seq[int] =
t.x
let t = create()
echo cast[int](t.x[0].addr)
let x = test(t)
echo cast[int](x[0].addr)
The background to this topic is that I wish to extract sequencies from an object and use them without a copy being made. I was hoping that 'lent' would offer a neater alternative to 'move'ing sequencies out and then back into an object.
This comes up again and again. I'm not sure why. When you do let x = test(t) the = does the copy as x's type is not lent T but T. This is exactly how the language works in other cases:
a[i] = 25
let x = a[i]
a[i] = 23
echo x # still 23