How can I change **initThing** so that it stores in **list** a _reference_ to
**original** so that when **original** is changed, **list** reflects the change.
The current code just makes a copy :-(
var original = @[1, 2, 3, 4, 5]
type
Thing = object
# I would like `list` to be set to a *reference *of `original`
list: seq[int]
proc initThing(list: seq[int]): Thing =
result.list = list
var thing = initThing(original)
proc show(label: string) =
echo "\n", label
echo "Original: ", original.repr
echo "Reference: ", thing.list.repr
show "Initial:"
original[0] = 666
show "Updated:"
if thing.list[0] != 666:
echo "\nNot a reference!"
Run
The above prints:
Initial:
Original: @[1, 2, 3, 4, 5]
Reference: @[1, 2, 3, 4, 5]
Updated:
Original: @[666, 2, 3, 4, 5]
Reference: @[1, 2, 3, 4, 5]
Not a reference!
Run
What modifications to **Thing** and **initThing** are required to make it show:
Initial:
Original: @[1, 2, 3, 4, 5]
Reference: @[1, 2, 3, 4, 5]
Updated:
Original: @[666, 2, 3, 4, 5]
Reference: @[666, 2, 3, 4, 5]
Run
This sounds simple but I tried all combinations of _ref_ , _var_ , _cast[]_ and
_addr_ that I can think of to no avail :-(
Any assistance (as usual) would be appreciated).