lua-users home
lua-l archive

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




On Wed, May 13, 2020 at 9:22 AM Andrea <andrea.l.vitali@gmail.com> wrote:
Yes, each time you enter a conditional block, do-block, or loop, you can declare variables that will go out of scope at the end of that block. Bonus fact: the iteration variables in for loops in lua go out of scope between iterations of the loop.
 :
If you are familiar with _javascript_, I believe this is the same as _javascript_'s let and const, and Python's behavior is the same as _javascript_'s var.

thank you very much for pointing that out - I was mislead by the reference manual which discuss scopes by using only do...end statements - in fact I was under the impression that scopes were created only at the function definition (kind of _javascript_'s var) and at do...end blocks (kind of curly braces in other languages).

Now forgive me for asking this, even if I am a programmer I am not a Computer Science Engineer. My question is: why are new scopes created by "if", "for" and other statements and not only by function definitions and do...end statements? If one wanted a new scope one could easily type "do" (as in other languages we just open a curly brace). So why this is done implicitly? What is the advantage or the theoretical reason? Again forgive me for this maybe silly question - remember my background is not in computer science.

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.

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