lua-users home
lua-l archive

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


Other users have correctly answered with some details for me, so I'm going to snip down my response to just the original parts.

On Tue, May 12, 2020 at 9:41 PM Andrea <andrea.l.vitali@gmail.com> wrote:
With local-by-default, if condition() is false the shadow function will print the "x" created outside, if condition() is true it will create a local x which is then printed. And this may be the case I was looking for. The difficult error to spot... right?

Yes, it can be a difficult error to spot. In fact, it's it's so tricky that I made a mistake here: I was assuming the scoping rules of Python or of nonstrict _javascript_, where only functions establish lexical scopes. In Lua (as in C and strict-mode _javascript_), any code block establishes a lexical scope. So depending on your scoping rules, the nonlocal version of the shadow function could print 1 or 2.

Furthermore, depending on the implementation, it's possible that the presence of "x = 2" there might create the local variable even if it's never executed. This sounds like a silly hypothetical but something similar is actually a common source of bugs in Python and nonstrict _javascript_ code. In this case, again depending on the implementation, it might print "nil"!

 
The problem with this syntax is that you still need a local keyword within functions in order to avoid namespace collisions. 

Again I am not sure I understand. If it is local by default, why would you need a local keyword? 

This was in response to having "global" in the outer scope, not to any of your comments. Ahmed has replied already to say that his intent is to require both global AND local, but I'll reply to his message separately.

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