lua-users home
lua-l archive

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


I've been experimenting with table introspection, specifically trying
to get the name for a given table object.  Here's a simple example:

function tableName(tbl)
assert(type(tbl)=="table", "tbl must be a table")
    for n, v in getfenv() do
        if v == tbl then
            return n
        end
    end
    return nil
end

t1 = { "a" , t2={"b"} }
print(tableName(t1))
print(tableName(t1.t2))

The first print results in the name "t1" being printed as expected. 
But the 2nd print produces nil.  Obviously t2 is not defined within
the calling environment when tableName(t1.t2) is run in the 2nd print
example.  (I could recursively descend every table in the calling
environment until I find t2--which seems expensive--and collect the
intermediary parent table names as I go to form a dotted name.)

But am I missing something?  Is there a built-in, design-intended way
of introspecting table names (and, for that matter, function names)?

Thanks,
Bill