lua-users home
lua-l archive

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


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