lua-users home
lua-l archive

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


On 21 Nov 2012, at 18:29, Dirk Laurie wrote:

> BTW the old rv1 is only an upvalue when the new rv1 is
> inside a function.  In the code you give, everything inside
> the repeat is still at the same call stack level, so the old
> rv1 is still just a local variable.
> 

This is my misunderstanding as I don't know enough about how lua works, to me there are 2 stack levels. I am also perhaps using the term upvalue incorrectly.

My original intention was for the following:

do
	local f = 1
	relocal f
	print(f) --prints 1 as the relocal does nothing
end

local f = 2
do
	relocal f
	print(f) --prints nil as the relocal creates a new local variable, masking the outer f, because there is no f in this scope level
end

Now I've read the responses I don't think this is necessarily a good idea, but it would have allowed me to mix my functions up the way I wanted, and stop me accidentally leaking into the global table.

The alternative, which as I understand it, means a relocal variable would only create a new local if the variable would otherwise be looked up in the global table (i.e. no locals/upvalues of that name) would have solved my problem, as well as David's.

Thanks,
Kevin