lua-users home
lua-l archive

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


On Fri, Aug 1, 2014 at 3:08 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 2014-08-01 20:47 GMT+02:00 Andrew Starks <andrew.starks@trms.com>:
>
>> On Fri, Aug 1, 2014 at 1:00 PM, Roberto Ierusalimschy
>> <roberto@inf.puc-rio.br> wrote:
>>>
>>> > It was mentioned that "table.clear" (or similar) was likely. Was it
>>> > decided
>>> > that it would not be useful?
>>>
>>> I don't think we even agreed on what it would do.
>>>
>>> -- Roberto
>>>
>>
>> The only negative I found was disagreement on why one would want it.
>>
>> Primary example:
>>
>> Shared state container between two entities that needs to be reset. As it
>> is, the solution would be to store the state inside another table and
>> replace that table. It'd might be better to clear it, thus creating less
>> trash and allowing me to more concisely represent what i'm doing.
>>
>> Another example, which was the source of comment by Mike:
>>
>> A pretty printer that needs to track tables that have already been
>> traversed. I would store the tostring(t) (assuming no __tostring) in a table
>> as `visited[tostring(t)] = true`.
>>
>> When done, I would clear the table. As it is, i would make the table new,
>> every time because I would assume that looping and nilling the values would
>> take more time that trashing the table.
>
> If there is no API function for this, then there is no call for having
> it in Lua.
>
> What would such an API function do? Traverse the whole table and
> set every value to nil, in order to adjust the reference count. It's not
> obvious that the performance enhancement relative to
>
> for k in pairs(tbl) do tbl[k]=nil end
>
> is worth yet another library function in a language that can't even carry
> the baggage of cosh, sinh and tanh.

Well, one reason for it may be LuaJIT which does not (will not)
compile pairs. It seems reasonable to me that wiping a table would be
easier to compile and therefore table.wipe would be desirable.

-- 
Patrick Donnelly