View Single Post
01-23-14, 07:14 PM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Resike View Post
You need to use select as (select("#", self.Events)) but don't get why would you want to use select in that function.
No. Please, stop posting misinformation. If you are not absolutely sure that what you are posting is valid for Lua programming in the WoW environment, preface it with an "I think" or don't post it at all.

The only thing wrapping a select call in parentheses does is truncate the list of returns to a single value (the first one returned) but when you're only assigning one variable -- eg. local var = select("#", ...) -- there is absolutely no benefit to or effect from doing that. The only reason to do wrap a select call in parentheses would be to something other than the first returned value, without assigning it to a variable, and in a context where the values passed after the one you wanted would cause problems. For example, if you have the following function:

Code:
function GetSomeNumbers()
     return 1, 2, 3, 4, 5
end
... and you wanted to loop from 1 to 3 using the return value from this function, you can do it two ways:

Code:
local _, _, n = GetSomeNumbers()
for i = 1, n do ...
Code:
for i = 1, (select(3, GetSomeNumbers())) do ...
Here in this highly contrived example, if you used the second (far less efficient) method, and did not add the extra parentheses (highlighted above in orange) around the select call, you would effectively be writing:

Code:
for i = 1, 3, 4, 5 do ...
... which would (a) be passing 4 as the increment value for the loop (so i would be 1, then 5, then 9, and so on) and (b) be passing 5 as a 4th value that for doesn't know what to do with and would trigger an error.

Originally Posted by Aanson View Post
Using "#" to get the number of indices of an array was not limited to varargs. It could be used for any array. It just so happens that a vararg expression is an array.
I think you're confused. In your example:

Code:
local events = { "one", "two", "three }
local num = select("#", events)
... num should be 1, since you are passing only one value -- your table. If you want to get the length of the table, you need to use the # operator, not the special string "#" with select:

Code:
local events = { "one", "two", "three }
local num = #events
That's how it's always worked.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote