lua-users home
lua-l archive

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


On Sat, Nov 10, 2012 at 11:49 AM, Wesley Smith <wesley.hoke@gmail.com> 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
>

Think for a moment why that wouldn't work -- the \t would only be
added to the first line of a subtable.

/s/ Adam