lua-users home
lua-l archive

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


On Wed, Jun 13, 2012 at 2:01 PM, Hans Hagen <pragma@wxs.nl> wrote:
> On 13-6-2012 20:14, Roberto Ierusalimschy wrote:
>>>
>>> Concerning (2): not all types have metatables so boolean is still
>>> somewhat of a problem then. (I can live with it.)
>>
>>
>> We can use the metatables for strings (if at least one argument is
>> a string)! This already works both in 5.1 and 5.2:
>>
>> mt = getmetatable("")
>>
>> mt.__concat = function (a,b)
>>   return tostring(a) .. tostring(b)
>> end
>>
>> print("result: " .. {})     --> result: table: 0x8172130
>> print(false .. " x " .. true)  --> false x true
>
>
> interesting, so what is the trick for
>
> local t = { 1, 2, "test", true }
>
> print(table.concat(t))
>
>
>
>
> -----------------------------------------------------------------
>                                          Hans Hagen | PRAGMA ADE
>              Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
>    tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
>                                             | www.pragma-pod.nl
> -----------------------------------------------------------------

Might I suggest that the language would produce an error, unless
table.concat calls tostring(value) on each element?

That is, concat always means string and as a convenience, table.concat
may always promote to string, but otherwise the language wouldn't do
that as a function of its awesomeness (bloat).

I might be missing something in your question clever however.

-Andrew