lua-users home
lua-l archive

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


> I though that I understood what "lexically scoped" means, but I am not sure
> anymore.

Yes, I think some confusion has arisen.

As I understand it, "lexical" scoping is a term used in opposition to
"dynamic" scoping, i.e. whether an identifier's value arises from its inmost
enclosing textual definition (as in C, Pascal &c.), or from its most
recently executed enclosing function (as in LISP, BBC BASIC &c.).

Example:

function a()
  local x = 2
  c()
end

function b()
  local x = 1
  c = function ()
    print(x)
  end
end

Under dynamic scoping, the example above will print 2, because x's local
definition scopes dynamically over a's callees. Under lexical scope it'll
print 1, because x is bound by the textual (lexical) enclosing definition in
b().

Of course Lua allows neither at the moment, because local variables are not
visible inside function definitions (except via upvalues). Nevertheless, Lua
is lexically scoped: you can tell because if you run the simpler example:

function a()
  local x = 2
  b()
end

x = 1
function b()
  print(x)
end

you get 1, because a's x is not visible in b.

What proponents of "full lexical scoping" are really asking for might be
better described as "full closures", i.e. closures in which the free
variables are not fixed (but only bound) at instantiation time, and in which
you don't need special syntax to access them.

-- 
http://sc3d.org/rrt/ | Academics age by degrees