lua-users home
lua-l archive

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


I am sorry I do not understand but this is due to my ignorance 

Where do lexical block start? They end at “end”, they can start with “do”.

Also I assumed lexical blocks start with the function definition and end at their end.

Does “then” introduce a new lexical block which can end with “else” or “end”? Is this the way it works in Lua?

   Andrea 


On Tue, May 12, 2020 at 8:12 PM Robert Burke <sharpobject@gmail.com> wrote:
On Wed, May 13, 2020 at 11:42 AM Andrea <andrea.l.vitali@gmail.com> wrote:
>>
>> Bonus challenge: Consider the following code with a "nonlocal" syntax:
>>
>> x = 1
>> if condition() then
>>   x = 2
>> end
>> print(x)
>>
>> Does it output 1 or 2? Compare this to the following:
>
>
> I would say "2" if condition() is true, otherwise "1". The "x" is local because there is no "nonlocal" keyword.

Well, we've made "x=1" before the if create a new local, but we've not
made "x=2" in the inner scope do that. Are we disallowing shadowing
variables within a function?

What about this other code:

if condition() then
  x = 2
else
  x = 1
end
print(x)

If we'd like this to print 2 or 1, then it seems like we've gotten rid
of lexical scope entirely and now have scopes only at the function
level. This has been a recurring source of pain for python developers
who I witness writing code like this:

x = my thing of interest which is not an http response
# bunch of code
ok = all(x.response_code==200 for x in responses)
# bunch of other code
do_something(x) # oh no, why is x now an http response?
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org
--
Andrea Vitali






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