lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 7/28/08, Patrick Donnelly <> wrote:
> Hello,
> On Mon, Jul 28, 2008 at 5:15 AM, E. Wing <> wrote:
>> I can modify the vector later and its changes will be reflected in the
>> matrix which is good. But I realized that the matrix can get released
>> by the garbage collector even though there might still be vectors that
>> have a pointer to it. So if I do this:
>> my_matrix = nil
>> collectgarbage()
>> print(my_vector1) -- I expect garbage or crashes
> I would be hesitant to say this is a good thing. From a C perspective
> it looks natural, but if this is to be used by a script writer (e.g.
> someone who uses tools like MatLab), this may seem very strange. In
> Matlab, they make copies of a matrix/vector when you reference it.
> There are places where you cannot simulate this entirely in Lua (e.g.
> passing the matrix to a function); but, having a vector still
> reference a matrix is odd.

You have a good point. I can see where people might think they get a
unique copy of a vector to play with. This problem came up however as
a side effect to making the simple case work:
matrix[1][2] = 3

>From the original explanation I received, I needed to return a vector
that could modify the original data in the matrix, otherwise the
assignment in the above case will get lost. It wasn't my intention to
make this work:
vector = matrix[1]
It just came into existence as a side-effect.

Assuming I want a copy behavior instead of a reference behavior, is
there a way to do this while still having the matrix[1][2] = 3 case