Thread Tools Display Modes
04-11-10, 08:33 AM   #1
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Variables notation

I've learned LUA from looking at working code and pick what
looks good to me.

Now I ran into a problem with what I thought to be right.

I thought

Code:
LibzzAddOnInit.AddOnTable.apps
is the same as

Code:
LibzzAddOnInit['AddOnTable']['apps']
I thought .AddOnTable and .apps would mean an extension to the local
variable LibzzAddOnInit and picked from local scope, too.

There are many examples and explanations using the dotted notation without making sure that the namecomponents was a local variable.

Now I got an errorreport that made me thinking. For one user the .apps part was set to nil but it was defined this way

Code:
LibzzAddOnInit.AddOnTable = LibzzAddOnInit.AddOnTable or {
  ['apps'] = {}
}
So it should never be possible to be nil. Or am I wrong ?
I did not find the reason. But changing to the other notation the error
is gone ... where is the fault in my thinking ?
[*] Bugreport : ProcWatch comments
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
04-11-10, 09:08 AM   #2
Foxlit
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 91
lua Code:
  1. LibzzAddOnInit = {AddOnTable={}};
  2. LibzzAddOnInit.AddOnTable = LibzzAddOnInit.AddOnTable or {
  3.   ['apps'] = {}
  4. }
  5. print(LibzzAddOnInit.AddOnTable.apps); -- nil

LibzzAddOnInit.AddOnTable.apps and LibzzAddOnInit['AddOnTable']['apps'] are indeed equivalent; it's just that your initialization does not necessarily create the apps key if AddOnTable isn't nil/false to begin with.
__________________
... and you do get used to it, after a while.
  Reply With Quote
04-11-10, 10:17 AM   #3
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Code:
local LibzzAddOnInit, oldminor = LibStub:NewLibrary(MAJOR, MINOR)

LibzzAddOnInit.AddOnTable = LibzzAddOnInit.AddOnTable or {
  ['title'] = 'LibzzAddOnInit',
  ['Debug'] = false,
  ['ConsoleColor'] = '444488',
  ['commPrefix'] = 'Libzz',
  ['iconSize'] = 14,
  ['apps'] = {}
}
This is the code used. And the first time the lib is loaded this should be
initialized I would guess ?
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
04-11-10, 11:56 AM   #4
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
Make sure that the thing you are indexing is a table.

lua Code:
  1. local corrupt = nil
  2. corrupt.lol = 1 -- this generates an error
  3. corrupt["lol"] = 1 -- this generates an error
  4.  
  5. local good = {}
  6. good.lol = 1 -- yay, this works
  7. good.lol.corrupt = 1 -- another error, because your indexing a number value
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.

Last edited by nightcracker : 04-11-10 at 01:15 PM.
  Reply With Quote
04-11-10, 01:05 PM   #5
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Originally Posted by nightcracker View Post
Make sure that the thing you are indexing is a table.

[highlight=lua]local corrupt = nil
corrupt.lol = 1 -- this generates an error
corrupt["lol"] = 1 -- this generates an error

local good = {}
good.lol = 1 -- yay, this works
good.lol.corrupt = 1 -- another error, because your indexing a number value[/hightlight]
my prob is not the first var ... I mean the following. so in your example
'lol' and 'corrupt' and not 'good'.
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
04-11-10, 01:05 PM   #6
lilsparky
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2007
Posts: 117
look at the bug report. AddOnTable is an empty table.

AddOnTable = <table> {
}

that's the basic problem...
  Reply With Quote
04-11-10, 01:09 PM   #7
Saiket
A Chromatic Dragonspawn
 
Saiket's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 154
Did an older version of "Libzz" not include that apps sub-table? Your current code inherits the entire "AddOnTable" sub-table if it was already created by an older version of the library.

You could use the same technique for all your sub-tables too:
Code:
local LibzzAddOnInit, oldminor = LibStub:NewLibrary(MAJOR, MINOR)

LibzzAddOnInit.AddOnTable = LibzzAddOnInit.AddOnTable or {
  ['title'] = 'LibzzAddOnInit',
  ['Debug'] = false,
  ['ConsoleColor'] = '444488',
  ['commPrefix'] = 'Libzz',
  ['iconSize'] = 14,
}
LibzzAddOnInit.AddOnTable.apps = LibzzAddOnInit.AddOnTable.apps or {}
  Reply With Quote
04-11-10, 03:49 PM   #8
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Originally Posted by lilsparky View Post
look at the bug report. AddOnTable is an empty table.




that's the basic problem...
Yes, but I just dont know how this could happen.
If it were nil ok, then I'd know why ... but an empty table
means it gets altered at somewhere ... and my guess its
changed in the global scope and not in my code ...
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
04-11-10, 04:02 PM   #9
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Originally Posted by Saiket View Post
Did an older version of "Libzz" not include that apps sub-table? Your current code inherits the entire "AddOnTable" sub-table if it was already created by an older version of the library.
The apps part is one of the first things added in the lib. I even installed
all addons the user had ... but here I could not reproduce it.
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
04-11-10, 08:29 PM   #10
lilsparky
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2007
Posts: 117
you sure that was the only error he had? if something else bombed out, that table could have been left dangling.
  Reply With Quote
04-12-10, 01:35 AM   #11
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
I provided him a version with the dotted notation replaced
and the error was gone but the addon still not working.
After replacing the older lib of the other addon of mine his
problem was gone and worked as it should.

Thats why I wonder how it's possible to get different results
when both notations should deliver the same.

And my guess it is changed in a global scope ...
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
04-12-10, 02:12 AM   #12
orionshock
A Wyrmkin Dreamwalker
 
orionshock's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 50
Originally Posted by Rilgamon View Post
I provided him a version with the dotted notation replaced
and the error was gone but the addon still not working.
After replacing the older lib of the other addon of mine his
problem was gone and worked as it should.

Thats why I wonder how it's possible to get different results
when both notations should deliver the same.

And my guess it is changed in a global scope ...
as Saiket said. it was an error in your lib upgrade path as your own explanation details.

When the other addon had and older version of the lib it didn't have the .apps sub table. Your primary addon had the update version of the lib that did.

Your code that you use dosn't account for upgrade paths correctly and thus the .apps subtable was missing after the upgrade.


LibzzAddOnInit.AddOnTable = LibzzAddOnInit.AddOnTable or {...}

^^ This code effectively translates to "Set this table key to itself if nil otherwise use this new table im declaring".

As your upgrading from one version of the lib to the next, the table already existed but from a version of the lib that didn't have the .apps key.
__________________
"I was there in the beginning... and things were very different back then" --An Echo from a time before.
  Reply With Quote
04-12-10, 05:02 AM   #13
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Originally Posted by orionshock View Post
When the other addon had and older version of the lib it didn't have the .apps sub table. Your primary addon had the update version of the lib that did.

Your code that you use dosn't account for upgrade paths correctly and thus the .apps subtable was missing after the upgrade.


LibzzAddOnInit.AddOnTable = LibzzAddOnInit.AddOnTable or {...}

^^ This code effectively translates to "Set this table key to itself if nil otherwise use this new table im declaring".

As your upgrading from one version of the lib to the next, the table already existed but from a version of the lib that didn't have the .apps key.
True.
I digged through all old versions of the lib and the oldest has only

Code:
LibzzAddOnInit.AddOnTable = LibzzAddOnInit.AddOnTable or {
  ['ConsoleColor'] = '444488',
}
So its my fault that apps is empty and throws the error. Still AddOnTable should never be {} and it should make no difference which notation I use.

But thanks for all the good ideas and tips ... That helps me to prevent this
in future. I'll rewrite the part so that every single value and table is checked to exist so that adding values in later versions is no problem

Additionally I'll stay away from dotted notation to make sure that when there is a problem that its from my code
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Variables notation


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off