Thread Tools Display Modes
11-01-10, 01:07 PM   #1
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Why do they enclose code within "useless" do statements?

While reviewing code I sometimes come across things like

Code:
do 
	function testfunction()
		--stuff
	end
end
Everytime I saw this I asked myself what this may be good for.
Why these useless do statements?

Could someone please enlight me? Thank you very much.
  Reply With Quote
11-01-10, 03:09 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Because they're not useless. A do block can limit scope just as a function or if statement can.

http://www.lua.org/pil/4.2.html
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
11-01-10, 03:10 PM   #3
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
http://lua-users.org/wiki/ScopeTutorial
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
11-01-10, 03:27 PM   #4
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Oke. That sounds logical. Thank you very much.

But ... shouldn't it be

Code:
do 
	local function testfunction()
		--stuff
	end
end
instead of

Code:
do 
	function testfunction()
		--stuff
	end
end
?

Isn't the version without the global function the same as without the do/end??
  Reply With Quote
11-01-10, 03:35 PM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
A global variable is a global variable. But the local function is only valid for that do block. (ie, you could have another variable of the same name elsewhere)

/edit: unless I'm showing my noviceness here
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
11-01-10, 04:03 PM   #6
NitraMo
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 24
Based on my limited lua knowledge, if there only is:
Code:
do
  function fn()
    -- Yeah, whatever here...
  end
end
Then the do should be useless, as the OP is pointing out, unless there is something magical done then. The do is just wrapping the function and the do scope will contain nothing as fn() is global.
Anyone know this?
  Reply With Quote
11-01-10, 04:23 PM   #7
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Here's an example:

Code:
local MyComplexFunction
do
	local count = 0

	function MyComplexFunction() 
		count = count + 1
		print(("MyComplexFunction was called %d times!"):format(count))
	end
end
Since MyComplexFunction was previously declared as a file-local variable, the definition within the do-block is now assigned to it. Had I prepended the definition within the do-block with the "local" keyword, then the function would have become a new variable which was only in scope within the do-block. Had I not declared the file-scope variable before the do-block definition, MyComplexFunction would have been declared and defined within the global environment table.

Using this mechanism, I can be sure that the variable "count" is only ever used for the implementation of MyComplexFunction, and can not be altered elsewhere.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
11-01-10, 04:37 PM   #8
Nobgul
A Molten Giant
 
Nobgul's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 693
I may be showing my noviceness, and or lack of understanding of the OP point but. Also incoming text crit =P

Err brain fart. but something like....
lua Code:
  1. If Nobgul = isin $1 then
  2. function Send_hello()
  3. ------stuff here.
  4. else do
  5. function You_Are_Not_The_One()
  6. ------stuff here.
  7.  
  8. end
  9. end


I could have swore the "do" was just there to tell the command to fire. I am pretty sure that is the way it is in C++ anyway. meaning that the do command will always fire and the rest of the function will continue to work its self out. where if you used just a else then only 1 or the other would fire.

lua Code:
  1. if Nobgul == isin $1 then do
  2.  
  3. function savename() then
  4. -----something here
  5.  
  6. function ParseNameAndIssueCommand
  7. -------- suff here

Basically what i am getting at is using the statment to
grab a name in chat
make sure the name matches a specific name, either way save the name
then if the name does match then issue the command or w/e
otherwise take the saved name and pass a diff command

else do
then do
etc..

Because there is also
do while


Anyway I think there may not be a need for it.
__________________
[SIGPIC][/SIGPIC]
  Reply With Quote
11-01-10, 05:40 PM   #9
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by nobgul View Post
<snip>
Anyway I think there may not be a need for it.
Did you even read what I posted? :P
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
11-01-10, 10:49 PM   #10
Nobgul
A Molten Giant
 
Nobgul's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 693
you posted before me and i didn't refresh the page lol.
__________________
[SIGPIC][/SIGPIC]
  Reply With Quote
11-02-10, 10:41 AM   #11
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by nobgul View Post
you posted before me and i didn't refresh the page lol.
D'oh! It all makes sense now!
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
11-02-10, 11:10 AM   #12
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
It's clear now. Thank's to everyone.
  Reply With Quote
11-04-10, 11:59 AM   #13
Mischback
A Cobalt Mageweaver
 
Mischback's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 221
*cough*

Sometimes... sometimes I simply add a "do ... end"-block to some code, just because that enables Notepad++ to collapse that block. Dirrrty hack there, I know, but since it won't hurt nobody...
__________________
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Why do they enclose code within "useless" do statements?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off