lua-users home
lua-l archive

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


2011/1/23 Pierre-Yves Gérardy <pygy79@gmail.com>:
> On Sun, Jan 23, 2011 at 19:13, Henk Boom <henk@henk.ca> wrote:
>> 2011/1/22 Pierre-Yves Gérardy <pygy79@gmail.com>:
>> I do think that with the new _ENV semantics there's more flexibility
>> to protect against the accidental use of globals, and that this is
>> worth exploring.
>>
>> However, I don't feel that declaring variables on use instead of on
>> creation is win. It ubfuscates scoping since it's harder to see
>> declarations. It also doesn't really make much sense for lua, since
>> upvalues are only introduced at function scopes while locals exist in
>> much more finely grained scopes. That is, upvalues and scope are
>> entirely different issues.
>
> I don't understand why you make such a difference. Upvalues are locals
> declared in the surrounding lexical scopes.

>From the refman:

> A local variable used by an inner function is called an upvalue, or external local variable, inside the inner function.

upvalue specifically refers to a variable being declared and used in
an inner function, not just any scope (i.e. it is what is "closed
over" by a closure). The distinction is important because of the
implementation details, in that the variable might have a lifespan
longer than the function within which it is defined (since the inner
function still refers to it).

Basically, all upvalues are outer local variables, but not all outer
local variables are upvalues.

    henk