lua-users home
lua-l archive

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


On 10/11/2012 20:49, Wesley Smith wrote:
To print an array, object, whatever... in "tree view" with indentation, we
need to pass a nesting/indent level, and also the set of previously visited
tables to avoid inf recursion. If the function performing tostring for each
type accepts these parameters and deals with them, but __tostring does not
forward them, then we need to check the type of each sub-item to explicitely
call this function, instead of relying on the magic __tostring mechanism.

For the indent level, can't you just have every node tostring() itself
as normal and then have the parent add an indent?  That way the indent
would accumulate and you wouldn't need any extra parameters:

function __tostring(t)
    local res = {}
    for i=1, #t do
     res[i] = '\t'..tostring(t[i])
    end
    return table.concat(res, "\n")
end

Sure, but this works for 1 level only. I need:
      res[i] = '\t'..tostring(t[i], level+1)
if ever t[i] is a table, to align its own child nodes 1 indent level further (...which themselves may be tables, etc).

Denis