lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Poking about I discovered that the metatable for _G (or _ENV) has a table called __declared whose keys are the names of user defined objects and value is always true.  This seems a useful thing for testing whether a function has been defined or a table exists. 

I am not aware of any documentation of this feature, __declared does not appear in the Lua 5.2 reference manual or in the index of PiL 3rd edition.  Is this a documented feature of Lua? Can I rely on this in the future?  Also do the __newindex and __index functions in the metatable do anything other than maintain __declared?

I discovered this in the process of studying whether I could create a function that incremented a counter for every access of a function.  I have in mind a function installed as the __index function of the metatable that would maintain a table similar to __declared except that it would increment a counter for the called function every time it was called.  if this function ended by calling the preexisting __index function would that work without breaking Lua?

Jose de la Torre-Bueno, Ph.D.
Empowered Energy Solutions Inc.
Intellectual Property & Technology Management
T (619) 977-0553
F (760) 295-7119
jtorrebueno@cox.net