[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: question on local vs nonlocal
- From: Roberto Ierusalimschy <roberto@...>
- Date: Wed, 13 May 2020 13:05:37 -0300
> It's ultimately a question of "why not?" Lua's scopes are bound to blocks,
> and a block is any structure that can contain one or more statements.
> There's no good reason to have a different type of block for an if...end
> statement than there is for a do...end statement. It lets the same rules
> define the syntax and the scopes.
I tend to think of 'local' as being an action that creates a variable.
Each time we execute that action, we create a new variable. Now,
consider this code:
if false then local x = 1 end
print(x)
It seems weird to me that 'print' would be using a variable that was
not created. (In this case, it was also not initialized.)
By bounding them to inner blocks, we ensure we can only use a
variable that was properly created.
-- Roberto
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org
- References:
- question on local vs nonlocal, Andrea
- Re: question on local vs nonlocal, DarkWiiPlayer
- Re: question on local vs nonlocal, Andrea
- Re: question on local vs nonlocal, Coda Highland
- Re: question on local vs nonlocal, Andrea
- Re: question on local vs nonlocal, Robert Burke
- Re: question on local vs nonlocal, Andrea
- Re: question on local vs nonlocal, Robert Burke
- Re: question on local vs nonlocal, Andrea
- Re: question on local vs nonlocal, Coda Highland