lua-users home
lua-l archive

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


This behaviour means that all earlier local declarations of the
variable become completely shadowed by later local declarations at the
same scope. I am afraid it makes code less readable.

local x="first declaration"
local x="second declaration" -- the same scope

The first line is now completely irrelevant, because its version of
'x' is not accessible by regular Lua means (except for debug library).

Is there any particular reason to design Lua local scope rules this way??

--Leo--

"because its version of 'x' is not accessible by regular Lua means"
Not entirely true.

-- typed in Mail.app; not tested; etc.
local x
local function getFirstX() return x end
local function setFirstX(n) x = n end
local x
local function getSecondX() return x end
local function setSecondX(n) x = n end
-- can now access either 'x' via the appropriate functions


As for how common such a coding pattern might be, I leave to others to debate :-)

-- Tim Gogolin