lua-users home
lua-l archive

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


Why not create such a function as a member of the string library?
string.concat( "there ", "I", " would look ",1, "st")

However, this does not solve the problem to define the separator as in table.concat(); for this one would still have to go through deifning a table and use table.concat():
table.concat( {"there", "I", "would look",1, "st"}, " ")

-- Oliver

Am 25.03.2014 05:32, schrieb Dirk Laurie:
2014-03-25 5:13 GMT+02:00 Andrew Starks <andrew.starks@trms.com>:

I also agree that ".." should always call __tostring, even on (especially) tables.
I don't agree. Lua should "not know what to do" so that a metamethod will be
looked for. Otherwise we can chuck away the __concat metamethod.

For example, with a simple "list" object:

a = list{1,2,3}
tostring(a) --> "{1,2,3}"
a..4 --> {1,2,3,4}, not "{1,2,3}4"

Having said that, I must admit that I've often wished for a builtin
Lua function that
simply concatenates all its arguments. It's available in the API and the VM, but
not in Lua. That makes it trivial to write in C for oneself, of course.

/* tuple.concat(...) */
static int tuple_concat(lua_State *L) {
   lua_concat(L,lua_gettop(L));
   return 1;
}

But I find it curious that this powerful Lua feature is not exposed at the level
of the scripting language.