[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Forward declarations in modules
- From: "Gavin Kistner" <gavin.kistner@...>
- Date: Thu, 7 Sep 2006 15:10:14 -0600
> It seems that there are actually 3 copies of "a" here, with different
> values. By the time I call f and g the most recent a is 30, however
> they seem to be "bound" to the local declarations earlier up.
>
> How does Lua distinguish each "a" from each other? Judging by the
> earlier error message it makes them upvalues of the functions as it
> hits the function declarations.
I was personally quite surprised by this result. It appears that 'local'
creates a new variable, even if another local variable with the same
name already exists.
local a = 10
function f( )
print( a )
end
local a = 20
function g( )
print( a )
end -- g
a = 30 -- no local here
print( a ) --> 30
f( ) --> 10
g( ) --> 30