I'm well aware of the idea of scoping, and how lexical scoping works in general. I've heard that the idea of a do ... end
block is to limit the scope of certain variables, but what's the point? When would this ever be necessary or even practical? It seems to me that the usage of local variables makes such blocks pointless, but maybe their uses aren't actually related?
Note: Sorry, I don't have any code to include. As I mentioned, I understand the syntax and the general idea of scoping, I'm just not clear on when a do ... end
block would be beneficial.
the main use i have found for "do (code) end" is limiting or setting the function environment of certain variables, for example locally setting an already made variable to something else for use inside of a certain part of the code that changed part color or some type of property in a part, or you could use get and setfenv for more fun variable changing
besides that i never really found a use for it
Yes you are right, but uses for that can be different even tho i don't use them too, you can use them as a decoration like:
01 | function () |
02 | do -- configuration |
03 | assert () |
04 | assert () |
05 | assert () |
06 | assert () |
07 | assert () |
08 | assert () |
09 | if not x then |
10 | return |
11 | end |
12 | end |
13 |
14 | -- and here the script goes |
15 | end ) |
Even tho you do not have to, also another use is for example having classes in one script and you would need to limit amount of variables so you do
01 | do |
02 | local class 1 = { } |
03 |
04 | function |
05 | class 1. test = true |
06 | end |
07 |
08 | do |
09 | local class 2 = { } |
10 |
11 | function |
12 | class 1. test = true |
13 | end |
14 |
15 | print (class 1 ) -- nil |
16 | print (class 2 ) -- nil |
And here you prevented uneccessary variables, overall it's mostly useless but i have found it a use once, for example i could split the code into modules but i needed one variable to be accessed from all the classes and defining it in every single module would be ehh so i used do for that:
01 | local ImportantVariable = "WideSteal" |
02 |
03 | do |
04 | local class 1 = { } |
05 |
06 | ImportantVariable.LoadClass(class 1 ) |
07 | end |
08 |
09 | do |
10 | local class 2 = { } |
11 |
12 | ImportantVariable.LoadClass(class 2 ) |
13 | end |
Instead of doing this in every single module
1 | -- module 1 |
2 | local ImportantVariable = "WideSteal" |
3 |
4 | -- module 2 |
5 | local ImportantVariable = "WideSteal" |
BUT here setfenv
comes instead, if every single module will have function Init
then you can get rid of do...end
01 | -- script |
02 |
03 | local CustomEnviroment = { |
04 | ImportantVariable = "WideSteal" , |
05 | print = print , |
06 | } |
07 |
08 | for module in pairs (modules) do |
09 | module.init(CustomEnviroment) |
10 | end |
11 |
12 | -- module1 |
13 |
14 | function module 1. init(CustomEnviroment) |
15 | setfenv ( 1 , CustomEnviroment) |
so basically do...end
has probably no uses except for decoration