View Single Post
07/09/14, 08:31 AM   #5
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Originally Posted by zgrssd View Post
For example, afaik "" is a valid value. But not a valid index. BugEaters inability to deal with nil and "" values pointed me towards that issue.
Anything but nil is a valid index (you can even use another table as key). In SavedVars only int/string/boolean keys are allowed, but that is SavedVars limitation, not Lua limitation. Empty string is a valid key in both.

Skip down to *CONSTRUCTIVE* if you only want something constructive, this below is just rant

Originally Posted by zgrssd View Post
A simple int indexed array of key&value allows a lot more ways to process the data.
But not the one way I find most convenient - quickly answering the question "what's the symbolic name of this value?".

Originally Posted by zgrssd View Post
Edit (too 3):
The more I think about it, the better my idea sounds. The global table is full of values that would never make a valid index for a table: empty stings, functions, other tables - non of that could ever make a proper index.
I don't think I would ask the name of anything but integer constants (except when I was writing a function to dump _G, I used some heuristics to give names to metatables, and that was when I used [{table}] => "string" mapping; but that's something completely out of scope of this library, I guess).

Originally Posted by zgrssd View Post
Plus sometimes the same function/table might be used as value multiple keys (so I had the same index used multiple times).
Clear index-worthy int and string values seem to be a odd thing in the global table, rather then a common one.
Look at it this way. You give me an array of pairs. I want to know the name of a thing. So I'll write this function:
Lua Code:
  1. local function nameIt1(value, mapping)
  2.     for i, m in ipairs(mapping) do
  3.         if value == m.value then
  4.             return m.key
  5.         end
  6.     end
  7. end

Someone else might instead do it this way:
Lua Code:
  1. local function nameIt2(mapping, value)
  2.     local name = nil
  3.     for i, m in ipairs(mapping) do
  4.         if value == m.value then
  5.             name = m.key
  6.         end
  7.     end
  8.     return name
  9. end

nameIt1 returns the first key with matching value, nameIt2 returns the last. Not to mention that the order of values in mapping depends on the order in which you get them from zo_insecurePairs! Which may be different every time you /reloadui, so addons using any nameIt function might behave strangely sometimes. I know it's hard do deal with conflicts properly, but if it's done in a library, at least it can be done consistently.


*CONSTRUCTIVE*

You could:
1) Add type requirement to your mapping spec. For example, "only int values allowed", everything else thrown away.
2) Sort your data={{value=5,key="foo"}, {value=5,key="bar"}} pairs table on value and key (sorting elements with equal values on key is important if you want consistent search results). Or zo_binaryinsert right away, instead of table.insert's and table.sort afterwards.
3) Provide a lookup function which would zo_binarysearch for given value in the sorted data.

Last edited by merlight : 07/09/14 at 08:34 AM.
  Reply With Quote