Doubt about modules and local keyword (SOLVED)

#1
local functions={}

   local function functions.test()
	local a="eee"
	print(a)
end


return functions

Need to put local in all the functions or variables, or a function/variable inside a table/function, are always local?

or if the table/function is local, al his “child” are local?

local functions={}

function functions.test()
	a="eee"  --or this need to be local?
	print(a)
end


return functions

thanks

0 Likes

#2

Second version is correct.

Note that the variable a in that case is global. You might want to transform it into functions.a or local if you don’t want any access to it from outside the module

0 Likes

#3

Then test function is local because its inside a table with local, but the variable is global because is inside a function without local keyword, but this fuction is local.

Im correct?

0 Likes

#4

You can’t compile this code.
function functions.test() already local as a field inside local table functions.

0 Likes

#5

in stack overflow said me that everything inside a local table is local.

then, in a module, there is no need to use local keyword?

local module={}

function module.a()  -- is local?
      variable="a"    --is local??
end

module.b="b"  -- is local??

function module.c()
   tablet={}  -- is local??
end

need some aclaration

1 Like

#6
local module={}

function module.a()  -- is local? yes, module is local
      variable="a"    --is local?? no
end

module.b="b"  -- is local?? yes, module is local

function module.c()
   tablet={}  -- is local?? -no
end
3 Likes

#7

More information about scopes:
https://www.lua.org/pil/4.2.html
http://lua-users.org/wiki/ScopeTutorial

3 Likes

#8

I understand now, thanks

1 Like