Is there way to have reference that survives CopyTable()?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Is there way to have reference that survives CopyTable()?

Post by Bugala » Fri Oct 04, 2019 11:54 am

Had a nasty bug to hunt until I realised where the problem was.

here is an example code which illustrates the problem:

Code: Select all

reftable = { a="1" }
tabletwo = { reftable = reftable }

tabletwo.reftable.a = 2

DebugPrint(reftable.a)


tablethree = CopyTable(tabletwo)

tablethree.reftable.a = 3

DebugPrint(reftable.a)

First I am making reftable and make it have var "a" that contains number 1.

Then I make Table two, which has reference to reftable.

Now I change tabletwo.reftable.a value to "2". Then I debugprint reftable.a value, which is now 2 as well, as it should be.

However, next I use copytable to make a copy (tablethree) of tabletwo.


When I now go and change value of tablethree.reftable.a to "3", and when I now debugprint reftable.a, value is still "2", when I wanted that to change into "3".


By otherwords, I would like to have a way to make a reference to reftable in such a way, that even when I use CopyTable(), it would still in this tablethree.reftable.a affect also the value of reftable.a.

Is this doable somehow?

Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Re: Is there way to have reference that survives CopyTable()?

Post by Bugala » Fri Oct 04, 2019 12:02 pm


Post Reply