Thread Tools Display Modes
01-08-10, 06:18 AM   #21
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by nightcracker View Post
<snip>
The most important part is to not take parameters you don't need in your function. <snip>
The use of "_" instead of "self" is not doing what you think it is. All you're doing is re-naming the parameter, making its origin obscure. You are not eliminating it.
__________________
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
01-08-10, 06:39 AM   #22
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
Originally Posted by Torhal View Post
The use of "_" instead of "self" is not doing what you think it is. All you're doing is re-naming the parameter, making its origin obscure. You are not eliminating it.
I'm sorry, I thought the underscore variable was defined in Lua as a dummy variable. Looked it up, and it's not, although it is a convention.
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.
  Reply With Quote
01-08-10, 06:40 AM   #23
Wobin
An Aku'mai Servant
 
Wobin's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 35
Originally Posted by Torhal View Post
The use of "_" instead of "self" is not doing what you think it is. All you're doing is re-naming the parameter, making its origin obscure. You are not eliminating it.
Indeed, people think '_' is a magic parameter that instantly discards what it's assigned as. In actuality, it's just another variable name like any other. It's used in common parlance for 'ignored' variables only because it's a nonstandard character, and easy to identify.

On another point, if 'self' is being garbage collected mid function, you've already got problems with your situation... mostly in that the rug just got pulled from under you. Remember, everything is passed in as a -reference-, you're not -really- making a full copy of 'self', so all that is 'wasted' is a local reference. And if a local reference is causing you memory issues, you clearly have more serious problems with your code.
__________________
Wob's Portal
  Reply With Quote
01-08-10, 09:42 AM   #24
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Originally Posted by nightcracker View Post
use function(_,event)
Just to clarify here, you're still creating two variables with the above. _ is a perfectly valid name for a variable. It's just that it's become common practice to use _ as a throwaway variable.


edit: wow... there was a second page (/me needs caffeine)
__________________
"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


Last edited by Seerah : 01-08-10 at 09:48 AM.
  Reply With Quote
01-08-10, 01:50 PM   #25
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Originally Posted by nightcracker View Post
lua Code:
  1. UIParent:HookScript("OnEvent",function(_,e) e=="DUEL_REQUESTED" and CancelDuel() and StaticPopup_Hide("DUEL_REQUESTED") end)
The problem I see with this code is I'm not sure if CancelDuel() has a return value, which would result as nil and fail to run StaticPopup_Hide(). It's just how Lua processes logical operators to speed up execution time.

Here's an example:
Code:
func1() and func2()
If func1() returns nil or false (no return is considered nil), it'll return that value and not call func2(). Otherwise, it'll return the value func2() returns. or works in the same way, only calling and returning func2() if func1() returns an equivalent boolean true.


Another problem I see with the above code and I haven't exactly tested it, is if you have an expression that isn't assigned to a variable or used as an argument in a function call. Lua would throw an error trying to parse the line, expecting an assignment operator. A single function call would be an exception to this rule. A conditional "and/or" line is never a good replacement for an "if...then" block.





Originally Posted by nightcracker View Post
The most important part is to not take parameters you don't need in your function. Instead of function(self, event, ...) use function(_,event). If you do take the parameters in your function they get stored in the memory(which is not bad at all), but then comes the garbage collector wasting CPU on what really IS garbage.
As stated in above posts, unused variable references would be the least of your worries when it comes to memory. It's also considered good programming practice to make code as readable as it is functional.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 01-08-10 at 02:13 PM.
  Reply With Quote
01-09-10, 12:03 PM   #26
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
After reading and thinking about it I finally figured where the "fishingpole"
error came from.
Code:
addon:AddConfigEntry("toggle",name,desc,_,1)
should have been
Code:
addon:AddConfigEntry("toggle",name,desc,nil,1)
Another mystery solved
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » local is local in a global?


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