lua-users home
lua-l archive

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




On Tue, May 12, 2020 at 1:50 PM Andrea <andrea.l.vitali@gmail.com> wrote:
It could be done, but again: lexical scoping. Using "nonlocal" would make _ENV less elegant and it would make closures more verbose, for no practical benefit.

can you clarify with an example how _ENV becomes less elegant?

same for closures: why should they be more verbose? 

A closure example:

function make_counter()
  local x = 1
  return function() x = x + 1; return x; end
end

This is very trivial, but this would require a "nonlocal" definition even in a simple example. _ENV is more or less a different way of approaching closures. A more complex closure just ends up needing more nonlocal declarations. As I said, this doesn't impact the correctness of it, but this is a case where you absolutely HAVE to have a declared local inside the outer scope, regardless of the syntax, so "nonlocal" can only make this code more verbose.

I am not sure I fully understand what you mean by "symmetric" behavior.

writing: "x = 1" can be an assignment to local, upvalue, or global in both cases; it depends if/how x was declared before

reading: " ... = x" means referencing local, upvalue, or global in both cases; it depends if/how x was declared before

function read()
  return x
end

function write()
  x = 1
end

Since neither of these have any declarations, I would hope that they would behave the same way with respect to the x variable -- either both local (which makes read() an error) or both global, but in a language with "nonlocal" then read() is global while write() is local.

/s/ Adam
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org