Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What are the practical applications of do-end blocks?

Asked by 4 years ago

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.

2 answers

Log in to vote
0
Answered by 4 years ago

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

0
yeah I can't say I've ever used get/setfenv either. it's not like they dont have their uses obviously, but I clearly haven't encountered a scenario in which they would be necessary. i guess thats the situation I'm in with do-end blocks Gey4Jesus69 2705 — 4y
Ad
Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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:

01function()
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
15end)

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

01do
02    local class1 = {}
03 
04    function
05    class1.test = true
06end
07 
08do
09    local class2 = {}
10 
11    function
12    class1.test = true
13end
14 
15print(class1) -- nil
16print(class2) -- 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:

01local ImportantVariable = "WideSteal"
02 
03do
04    local class1 = {}
05 
06    ImportantVariable.LoadClass(class1)
07end
08 
09do
10    local class2 = {}
11 
12    ImportantVariable.LoadClass(class2)
13end

Instead of doing this in every single module

1-- module 1
2local ImportantVariable = "WideSteal"
3 
4-- module 2
5local 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 
03local CustomEnviroment = {
04    ImportantVariable = "WideSteal",
05    print = print,
06}
07 
08for module in pairs(modules) do
09    module.init(CustomEnviroment)
10end
11 
12-- module1
13 
14function module1.init(CustomEnviroment)
15    setfenv(1, CustomEnviroment)
View all 24 lines...

so basically do...end has probably no uses except for decoration

0
i dont think its for "decoration", but I appreciate the samples Gey4Jesus69 2705 — 4y

Answer this question