Page 1 of 1
lua question
Posted: 29 Dec 2012, 06:40
by smoth
file.lua
Code: Select all
local choices = { "choice1", "choice2", "choice3",}
-----------------------------------------------------------
-- Retrieves bucketChoice table
-----------------------------------------------------------
function Choices()
return choices
end
gadget.lua
Code: Select all
VFS.Include('file.lua')
local choices = Choices()
Now, it does pass the table, I know this much.
however, is it passing a reference or is it passing the value when I get the return in gadget.lua? Because my understanding is localizing creates a limited scope or does including flie.lua mean that it has a local choices as part of the gadget? The reason I ask is because I cannot access choices without creating a function to return it(WHICH imo is a good thing)
You can guess I am trying to see how I can get some kind of half assed private and public stuff going. (in before lua OO, I am still not conviced that use of metatables is needed)
Re: lua question
Posted: 29 Dec 2012, 13:53
by gajop
Most likely reference as that's how tables are passed in lua, hiding the variable doesn't matter, as you are not referencing _G["choices"] (which doesn't exist), but the value of the variable set as the result of Choices().
Re: lua question
Posted: 29 Dec 2012, 15:24
by jK
Code: Select all
local t = {1,2,3}
function GetReference()
return t
end
function GetNew()
return {1,2,3}
end
Re: lua question
Posted: 29 Dec 2012, 16:11
by smoth
so it is returning just the value(s)? or a reference? It looks like it is returning the reference.
Re: lua question
Posted: 29 Dec 2012, 16:19
by FLOZi
Yes reference.
Re: lua question
Posted: 29 Dec 2012, 16:26
by smoth
So any function that I localize doesn't get "pulled in" with the non localized functions on an include?
How does this scoping work? because obviously the functions can access the variables inside of file.lua but gadget.lua cannot without a helper function?
Re: lua question
Posted: 29 Dec 2012, 16:35
by jK
TABLES ARE ALWAYS PASSED BY REFERENCE
The only thing that matters is if the table is a new created one or if it is a `cached` variable:
Code: Select all
local v = {1,2,3}
local f = function() return 1 end
local s1 = "foo"
function foo()
local w = v --// you get the reference to v
local g = f --// you get the reference to f
local n = {1,2,3} --// creates a NEW table (each run of foo)
local h = function() return 1 end --// creates a NEW function (-> damn slow operation!)
local s2 = s1 --// both s1 & s2 point to the same memory location, but once you modify one of them, they create a new memory construct and KEEP the old one, so changes aren't copied between s2 & s1
local s = "foobar" --// if "foobar" isn't in the internal string-cache, lua will create a new one with its content. But the string is always passed by value.
end
Re: lua question
Posted: 29 Dec 2012, 16:48
by smoth
So the values returned, where is the local var that they reference? that stays alive in mem until it is garbage collected? If I alter the value returned it does *NOT* alter the original but instead creates a new version in memory? so in your s2_s1 example, I can alter s1 without altering s2?
Also, I am trying to understand how this localization scoping works with regards to includes because I am moving a tedious bit of code into a utility library and when I include said utility library. That utility library has a bunch of helper stuff that not everything needs. Does lua only pull in what it calls out of the include? does it pull in everything? To me it only seems to pull in non-localized things, functions variables etc.
If this is the case the functions that I am calling, are they non-localized? how are they treated?
Re: lua question
Posted: 29 Dec 2012, 16:55
by jK
Strings are
By Value.
Code: Select all
local t = {1,2,3}
local y = {4,5,6}
local w = y
w[3] = 666
Spring.Echo(y[3]) --// prints: 666
t = 1
y = nil
Spring.Echo(t, y, w) --// prints: 1, nil, table
--The table created for `t` gets freed with next GarbageCollector calling.
--The table created for `y` is still referenced by w! It won't be freed.
Re: lua question
Posted: 29 Dec 2012, 17:01
by smoth
So every time a string is altered it creates a new construct(strings are immutable)?
What about the functions and their localization?