View Single Post
11-10-12, 02:06 AM   #12
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
The addon-level vararg (...) always contains two values:

1. A string value containing the name of your addon's folder.
2. An empty table.

You can modify the contents of the table, but you cannot change which string or which table the vararg contains.

First, you're assigning those two values to the variables addonName and addonTable:

Code:
local addonName, addonTable = ...
Next, you're adding two empty tables to the addonTable table, with the numeric keys 1 and 2:

Code:
addonTable[1] = {}
addonTable[2] = {}
Then you're using the select function to get a reference to the second value in the vararg, instead of just using the addonTable variable you already assigned that value to.

Then you're using the unpack table to get references to the values in that table with the numeric keys 1 and 2 (unpack returns all values in a table with sequential 1-based numeric keys), instead of just looking them up directly by key.

Then you're assigning those values back to the variables F and V.

Hopefully with this spelled out, you can see how convoluted what you're doing is.

If you want your addon's private table to contain two other tables, here is a more straightforward, more efficient, and less confusing way to do that:

Code:
-- Assign local reference names to the contents of the vararg:
local addonName, addonTable = ...

-- Create the two other tables with local reference names:
local F, V = {}, {}

-- Add those tables to the table that was the second content of the vararg:
addonTable[1], addonTable[2] = F, V
This is more straightforward, does not require any function calls (which are the slowest things you can do in Lua), and requires fewer table lookups (which are pretty fast, but definitely not free).

Also note that unpack ignores key/value pairs where the key is not a sequential integer. If you have this table:

Code:
local myTable = {
     [1] = "one",
     [2] = "two",
     [3] = "three",
     [7] = "SEVEN!",
     [8] = "eight",
     ["cat"] = "meow",
     ["dog"] = "woof",
     ["cow"] = "moo",
}
... and you call unpack on it, you will only get the values "one", "two", and "three" since those are the only values whose keys are sequential integers beginning from 1.
__________________
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.

Last edited by Phanx : 11-10-12 at 02:11 AM.
  Reply With Quote