Lua imported global and local variables with the same name

47 views Asked by At

I have a file module.lua in which I have a ( or 2? ) variable(s).

-- module.lua

test = 30
local test = 20
test = 40

print("module.lua: " .. test)

I also have my main file:

-- main.lua

require("module")
print("main.lua: " .. test)

Result of lua main.lua:

module.lua: 40
main.lua: 30

I was playing with lua variable scopes but I no longer understand What's happening here.

What happens when I have a Global variable and a local variable with the same name? (and then import that file in another one)

I guess after the local keyword, changes to the test variable are no longer global, But everything happened before it is Global. What is the use of this? why doesn't the interpreter throw an error and prevent this confusing situation? (I'm new to lua, came from the python world)

-- Trying this clearly says that changes made to test before local are global:

-- module.lua

test = 30
test = 70
local test = 20
test = 40

print("module.lua: " .. test)

lua main.lua ->

module.lua: 40
main.lua: 70
0

There are 0 answers