lua-users home
lua-l archive

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


On Tue, Nov 19, 2013 at 2:17 AM, Jose Torre-Bueno <jtorrebueno@cox.net> wrote:
For debugging outputs I have occasionally wanted to ask a variable its name.  I don't know a direct way to do this but I notice that the type of the values given to __index are a table and a string.
Following that lead I discovered that the following will give me a variable's name as a string

> tricktable = {__index = function (_,v) return v end}
> setmetatable(tricktable, tricktable)
> a = 1
> =a
1
> =tricktable.a
a
> =type(tricktable.a)
string

This looks like a misunderstanding of table indexing. tricktable.a is equal to tricktable['a'] - you're already creating a string here; it has nothing to do with any variable named a. You could change that name to anything else and it'll come back the same.


--
Sent from my Game Boy.