lua-users home
lua-l archive

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


2014-03-27 1:26 GMT+02:00 Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>:
>> Why not create such a function as a member of the string library?
>> string.concat( "there ", "I", " would look ",1, "st")
>
> How is that different from just this?
>         "there " .. "I" .. " would look ",1 .. "st"

function foo()
   return "there ", "I", " would look ",1, "st"
-- a real example would do some work to get there, and would
-- several return statements, not equally many values each time
end

table.concatenate{foo()} -- does not respect metamethods, must
   have strings or numbers
string.concat(foo())         -- concatenates respecting metamethods

But calling it string.concat misses my point.

There is no reason for __concat to have anything to do with
strings; on the contrary, I was thinking of its use for lists.
The API function / VM instruction is there. It applies any
right-associative function to values on the stack, provided
thatthe function is supplied as a __concat metamethod.

It is ideal. But it is not accessible from a script.