lua-users home
lua-l archive

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


On 11/20/2012 11:40 PM, TNHarris wrote:
On Tuesday, November 20, 2012 06:03:31 AM David Favro wrote:

local rv1;
repeat
     relocal rv1, rv2 = foo();
     until rv2;
-- use rv1 here...


But now you're saying that rv2 is a relocal when it's really just local to the
repeat block.
I thought that was the proposed meaning of the keyword: _if_ the name already exists as a local (or upvalue), then the keyword has no effect; but if the name would otherwise refer to a global (_G/_ENV), then it instead creates a new local at the current scope. But perhaps I'm mistaken. At any rate, as I said, I'm not necessarily in favor of it, but I do wish there were a more convenient way, in the same statement, to declare new local variables and assign to existing ones (given Lua's multi-valued expressions). It perhaps seems like a more obvious need to me than some because I typically code Lua with almost no globals, as opposed to traditional Lua style. This is also the source of the OP's desire for the keyword: he wouldn't want it if he didn't require all of his functions to be local.

> And what happens if you add a `local rv2` in the outer block;
you've broken the locality of the loop variable.

That's true and can be a bug waiting to happen, but that's really unrelated, it's a consequence of scoping/shadowing of names in general: you could make the same argument about any access to a global name from within an inner scope; the "globality" is broken when a local with the same name is declared in a containing block (or earlier in the same block), which is even more likely to cause a bug. In general, the referent of any reference to a name is likely to change any time that a new entity is declared with the same name. We mitigate this risk by using the 'local' keyword, but even then we can still break the code with a new 'local' declaration of the same name between the existing declaration and a later reference.