If I run this piece of code
MyTable = {}
function SomeFunction()
return MyTable
end
SomeFunction()
Does it create a new table or return a pre-created table?
So far, I don't know exactly how I can check that with current methods. I know
Let's walk through the execution of the code:
{}
MyTable
SomeFunction
SomeFunction
is accessedSomeFunction
:
MyTable
is accessedSo no, SomeFunction
will not copy MyTable
- it will return the reference to the already existing table. For the creation of a table inside SomeFunction
, you would need a table constructor inside SomeFunction
: function SomeFunction() return {} end
would create a new table each time it is called.
As Ramon has pointed out, you can use print(MyTable)
to print the reference to a table (e.g. to verify that it doesn't change).
You might want to localize MyTable
to make it an "upvalue" rather than a "global variable".