Table iteration question
I was cleaning up some of my code when I noticed something that might be obvious, but I didn't realise before: Attempting to access variables that do not exist by iterating a table and calling functions on them does not result in any errors.
For example: Lua Code:
This doesn't cause any errors. Why is this? I find it rather counter-intuitive. I'm now going over all my code where I attempt to access global variables through tables and making sure I do a _G lookup by name instead - otherwise, I never know when one of the frames I'm trying to access no longer exists. |
They do fail, only silently ;)
|
It doesn't work exactly like I think that you think it does. What happens here is, when the variables are nil, there will be no entry created in the table, and so, nothing to iterate over. If both of those are nil, it will simply be an empty table.
|
Quote:
However it's intresting, because you would expect that the pairs call would throw a nil error but it doesn't. Probably there is a missing nil check in the pairs function, about it's intended or not i'm not sure. |
I see, thank you.
It's easy enough if my table contains only single frames - I can make sure they exist by using a _G lookup by name. What if I'm also trying to refer frames that are table values of those frames, though? Let's say I have something like this: Code:
for _, bu in pairs({BonusFrame.RandomBGButton, BonusFrame.Arena1Button, BonusFrame.Arena2Button}) do |
How about to re-write like this?
Code:
|
Or this:
Lua Code:
I do exactly this for disabling the textures for the stock OverrideActionBar in my action bar addon: https://github.com/p3lim-wow/pAction...rd.lua#L58-L75 |
Quote:
Code:
> type({nil, nil}) |
Tageshi: That's a possibility, but it seems a little wasteful using function calls for what can be done using just table lookups.
p3lim: That's what I was looking for, thank you! Sometimes the solution is easier than you expect it to be. :) |
All times are GMT -6. The time now is 02:41 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI