lua-users home
lua-l archive

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


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"!

Thank you very much for confirming. Now it is clear. I was mislead by the reference manual. I thought that only function definitions and do...end statements create a new scope. I was wrong. Now I am curious to learn why this is not the case (see the same question in my other email in the same thread). Why scopes are automatically created whenever I am in the then or else clause. Why is this needed or useful. 

(I am not suggesting to change anything in Lua - I love as it is now - I am just asking to understand better why things are the way they are - thank you very much for your patience - if my questions are too obvious please point me to the literature I can read - I do not want to add noise/clutter to email threads)

   Andrea

--
Andrea Vitali






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