Fizzie, if I had control of the API, you’re completely correct.
The problem is that Blizzard’s API isn’t alway consistent in how it deals with no data.
Sometimes you get an empty table. Sometimes you get an actual boolean result. Sometimes you get a nil.
None of that is well documented.
I did it in self defense against the multitude of Blizzard stupidity out there.
Virtually every other language I’ve used that had anything remotely like the structure Lua has had either a set-value-to-this function or an override available for that function that could handle any data type.
I just did what was needed to avoid a hard error.
All I have to do with my function is test that the return results are in fact a table.
Table - got a good copy.
Not a table - nothing was there.
Additionally, in some instances I need to put something into a table that isn’t. There are times when you get, for instance, one power type back for a given unit type and times when you get more than one (this is just an example and not a particularly well thought-out one). If I’m trying to code for the general case, I need to code for tables. But it makes little sense to look up the available power types for things like Hunters or Warrior because they only ever have one active. I table those up as single value items.
Dealing with Druids is more complicated.
But rather than have completely separate processes for Druids and Hunters (again, for example) I have one and it drives from tabled data.
The process that determines what power type is appropriate doesn’t know until run time which class is being run and is adaptable so it grabs the data and the uses xCopyTable to put it into the common table format that I use elsewhere.
Having ONE “copy the power type” process that always produces tabled output regardless of the input is useful.
There are many, many places where this is true.
Occasionally, I get a nil value in when I can’t process it. Rather than get a hard error, I control the error by testing for it on output.
I might not know until run time that I’ll GET a nil output. The print functions you saw there are far less than what I’m actually doing. I maintain an internal error table. What really gets returned is a reference to that table if an error occurs.
I might choose to print that very message in debugging mode. Generally what happens is that my code recognizes a case that is outside of expected norms.
Things like a character that hasn’t yet reached a point where it can choose a specialization, for instance, or Pandaren who haven’t yet chosen a Faction. 99.9% of the play life of that toon, that won’t be an issue, but for the 0.1% that it is, I don’t want my addon puking in an ungraceful way.
Essentially this is so I can control the failure.
Yes, if I have to have data, it will fail.
But having to code in 1,000 place the “is there data here and is it in the right format” is just asking for errors.
I put all of that in one place and if it throws an error, THEN I branch to deal with it.