Thread Tools Display Modes
04-16-13, 08:10 AM   #1
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
CallBackHandler, select(1,

Just saw that CallBackHandler-1.0 is using the following to call functions:

Lua Code:
  1. if select("#",...)>=1 then
  2.     local arg=select(1,...)
  3.     regfunc = function(...) self[method](self,arg,...) end
  4. else
  5.     regfunc = function(...) self[method](self,...) end
  6. end

Given how often functions can be called through CBH (i.e, all Ace addons), could this be made more efficient?
  Reply With Quote
04-16-13, 09:22 AM   #2
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
You could probably do something like this and get rid of both select statements..
Lua Code:
  1. local arg = ...
  2. regfunc = arg and function(...) self[method](self, arg, ...) end or function(...) self[method](self, ...) end

Since they're just using select to check if there are any arguments in ... then assigning arg to ... will make it return nil if it has none, we can just check if arg is nil instead of using select.

Last edited by semlar : 04-16-13 at 09:33 AM.
  Reply With Quote
04-16-13, 09:26 AM   #3
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
Originally Posted by semlar View Post
You don't need to put parentheses around it since it would only assign one item to the variable anyway.

You could probably do something like this and get rid of both select statements..
Lua Code:
  1. local arg = ...
  2. regfunc = arg and function(...) self[method](self, arg, ...) end or function(...) self[method](self, ...) end

Since they're just using select to check if there are any arguments in ... then assigning arg to ... will make it return nil if it has none, we can just check if arg is nil instead of using select.
True true
  Reply With Quote
04-16-13, 10:43 AM   #4
Nevcairiel
Premium Member
Premium Member
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 63
While this may not be the most optimized code, it is only in the registration function, and not the actual callback handling functions, so overall efficiency shouldn't be hurt much.

Oh, and btw, testing for "... != nil" is not the same as calling "select('#',...) >= 1"
The first will not allow a "nil" in the value list, while the second will, which is the reason it was done.

Granted, the select(1,...) is pointless, but the select('#',...) is useful.
  Reply With Quote
04-16-13, 10:44 AM   #5
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
In the function docs above that bit of code:

Code:
	-- Registration of a callback, handles:
	--   self["method"], leads to self["method"](self, ...)
	--   self with function ref, leads to functionref(...)
	--   "addonId" (instead of self) with function ref, leads to functionref(...)
	-- all with an optional arg, which, if present, gets passed as first argument (after self if present)
So this is only executed when you are registering the callback.
__________________
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
04-16-13, 10:52 AM   #6
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Originally Posted by Nevcairiel View Post
Oh, and btw, testing for "... != nil" is not the same as calling "select('#',...) >= 1"
The first will not allow a "nil" in the value list, while the second will, which is the reason it was done.
I thought about this, but should it really be passing nil for the first argument?

I haven't looked at any more than the snippet provided here, but if the first argument is nil are you sure you want it being fed into the callback function?
  Reply With Quote
04-16-13, 11:04 AM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
One thing that select("#",...)>0 will detect that (...)~=nil won't. Note I changed the second one to ~=nil since just sticking (...) in an if statement will throw out boolean false. The point is that select("#",...) will increment if the function gets nil passed to it. This is in case you want to do something different depending if the user deliberately passes nil or just leaves the argument empty.
__________________
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)
  Reply With Quote
04-16-13, 11:18 AM   #8
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
I understand the use of select('#', ...) here, and I suppose it's for consistency that it passes an explicit nil to the callback function.

I was aware of this when I wrote my example, I just assumed if someone was providing nil as the optional argument that they didn't actually want it to be there.

However, I can see the case where if you're passing a variable to the function then you would be expecting it to hold the same value in the callback function, even if it's nil.

edit: My example wouldn't have worked if you were passing false to it either so it would need to be more complicated anyway.

Last edited by semlar : 04-16-13 at 12:04 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » CallBackHandler, select(1,


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