lua-users home
lua-l archive

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


2014-01-31 Andrew Starks <andrew.starks@trms.com>:

> [1] I wonder if discovering that there is a string metatable and that you
> can play with it is a common rite of passage amongst people who journey
> through lua...

I know of at least one more ...

About a year ago, John Hind and I were playing with an extended version
of the table library.

<https://github.com/dlaurie/xtable>

It contains a modified version of table.concat that applies tostring() to its
arguments instead of checking that they are numbers or strings.

Among the applications, we had:

Universal string concatenation
------------------------------

Define a metamethod for string concatenation as follows:

~~~
    universal = function(a,b) return tostring(a)..tostring(b) end
    getmetatable"".__concat = universal
~~~

This concatenates anything provided that the separator is a string.
A simple example is included in `test-xtable.lua`.

The universal concatenator is extremely powerful and it is a good
idea to detach it from string `__concat` when not in immediate use.

I was much more in love with this idea than John was, and eventually
`test-xtable.lua` did not make it to GitHub.