lua-users home
lua-l archive

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

2018-07-23 11:55 GMT+02:00 Luiz Henrique de Figueiredo <>:
>> One place where the symmetry currently is broken is concatenation.
>> The C API function lua_concat respects metamethods, the table.concat
>> function does not.
> The fair comparison is lua_concat vs the concatenation operator: both
> respect metamethods.

The problem with the concatenation operator is that it concatenates
just two values. If I concatenate everything in a table, respecting
metamethods, by the algorithm [1]

   local s = ''
   for k,v in ipairs(tbl) do s = s..v end

it takes O(n²) time, where n=#tbl, not to mention the memory problems.

To write an efficient pure Lua routine is possible, but quite challenging.

[1] The present implementation of 'lub.join' in the LuaRock 'lub' does
just that. I became aware of the problem when using the LuaRock
'xml' which depends on 'lub.join'.  For that application, the workaround
lub.join = table.concat is possible, but there may be lub applications
that rely on metamethods being used.