lua-users home
lua-l archive

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

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
Where "x" value would be actually read from: "_ENV.x" or upvalue "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.