lua-users home
lua-l archive

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


> Ah. I see. Actually, this works like normal variable lookup
> at the point of the function definition, right? (I.e.
> shadowing is in effect.)

Yes.


> Firstly, why can variables be local to the main chunk? As far as I can
> see, there is no way to access them?

What do you mean? What about that?

  local s = 0
  for i=1,10000 do s=s+i end
  function f(x) return %s + x end


> And secondly, why can inner functions refer to global variables
> only when they aren't shadowed by a variable local to a
> surrounding scope? Is this an implementation issue, or is it
> simply assumed that users want to use the local ones, and
> they can't without upvalues?

This is an arbitrary restriction. When someone writes something like

  function f(x)
    function g(y) return x+y end
  end

there is a high chance that the `x' he wants is the parameter to f. If
he really wants the global, it is easy to change the local name (and
then to avoid mental clashes...).

-- Roberto