lua-users home
lua-l archive

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




Am 21.07.2018 um 09:51 schrieb Egor Skriptunoff <egor.skriptunoff@gmail.com>:


Dirk, I have a surprise for you: you are among the people who would have benefits from introducing "\" syntax for globals; you just didn't realize it yet.
 
Your favorite trick with "function(_ENV)" is quite dangerous.
What would happen if some local in outer lexical scope has the same name as one of your globals?
 
   local x
   ...
   local function fun(_ENV)
     return x^2 + y^2 <= r^2
   end
 
Where "x" value would be actually read from: "_ENV.x" or upvalue "x"?

The language definition is crystal clear in this regard.  It would (and does) take the upvalue x, not _ENV.x.

 
This is a hard-to-find mistake.
The solution is to use "\" to explicitly claim that you want "x" from "_ENV", not from an accidental upvalue.
It's possible that the upvalue doesn't exist for now, but might be added in the future, and you would probably not notice this mistake (if not protected by "\" syntax).
 
IMO, "\" syntax is quite a good suggestion.
You just have to spend some time and mental efforts to create new habit.

It clutters the source code and makes it harder to read, imo.