lua-users home
lua-l archive

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


> 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