lua-users home
lua-l archive

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


Do you think Lua is lexically scoped?  I think I know how to resolve 
this issue.  Please identify the statements you find disagreeable.

John

  1. A variable can be accessed or modified.
  2. Scoping rules determine a variable's accessibility.
  3. Lua variables have either global or local scope.
  4. Lua has nested functions.
  5. In the following Lua program, the variable x is inaccessible on
     line 3. 

     1. function addn(x)
     2.   function sum(y)
     3.     return x + y   -- x is inaccessible here
     4.   end
     5.   return sum
     6. end
     7. print((addn(3))(2))

  6. A nested function has no access to variables defined in its
     enclosing functions.
  7. The upvalue construct gives nested functions access to copies of
     variables in its immediately enclosing function, but not access to
     the actual variables.
  8. In the following Scheme program, the variable x is accessible on
     line 3. 

     1. (define (addn x)
     2.   (define (sum y)
     3.     (+ x y))       ; x is accessible here
     4.   sum)
     5. (display ((addn 3) 2))

  9. In Scheme, a nested procedure has access to variables defined in
     its enclosing procedures.
 10. In a language with nested scope, a variable's accessibility is
     bounded by keyword pairs that mark the variable's region of
     accessibility.
 11. If Lua had nested scoping, the region of accessibility of variable
     x would be between lines 2 and 5.
 12. Lua variables lack nested scope.
 13. The region of accessibility of the Scheme variable x is between
     lines 2 and 4.
 14. Scheme variables have nested scope, and Scheme is lexically scoped.
 15. The description of lexical scoping, given by
     http://www.htdp.org/2001-01-18/Book/node104.htm, shows examples of
     nested Scheme procedures that access variables defined in their
     enclosing procedures.
 16. The FOLDOC definition for lexical scope follows: 

          <programming> (Or "static scope") In a lexically scoped
          language, the scope of an identifier is fixed at compile-time
          to be the smallest block (begin/end or function/procedure
          body) containing the identifier's declaration. This means that
          an identifier declared in some block is only accessible within
          that block and from procedures declared within it.

 17. The last sentence in the FOLDOC definition implies that, in a
     lexically scoped language, a nested function has access to
     variables defined in its enclosing functions.
 18. A language that has nested functions, but lacks nested scoping is
     not lexically scoped.
 19. Lua is not lexically scoped.
 20. Many people define the term static scoping to mean the same thing
     as lexical scoping. Under this definition, Lua is not statically
     scoped.
 21. The FOLDOC definition implies that Lua is not statically scoped.
 22. Others define the term such that a language is statically scoped
     even if it has nested functions, but lacks nested scoping. Under
     this definition, all versions of Python, and Lua are statically
     scoped.