lua-users home
lua-l archive

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


Edgar Toernig <froese@gmx.de> writes:

> He?  Of course they have nested scope.  Or how would you call this?

Your example show code that demonstrates nested scoping, but the
following code shows that Lua variables lack nested scope.

1.     function addn(x)
2.       function sum(y)
3.         return x+y      -- illegal reference to x
4.       end
5.       return sum
6.     end
7.     print((addn(3))(1))

If the variable x had nested scope, the scope of variable x would be
delimited by the function/end keyword pair.  It would start at line 2,
and continue until line 5.  With nested scoping, the reference to
varible x on line 3 would be legal.

Lua variables have either global or local scope.  A locally scoped
variable is accessible only within the function in which it is
defined.  Variable x is defined in addn, so its scope is not delimited
by the function/end keyword pair.  All regions that define a nested
function are removed from its scope.

In summary, the existence of local scoping and nested functions means
that Lua lacks nested scoping.

John