lua-users home
lua-l archive

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

On Tue, Mar 8, 2011 at 1:23 PM, steve donovan <> wrote:
On Tue, Mar 8, 2011 at 1:56 PM, Michal Kottman <> wrote:
> The solution is to use tostring explicitly and call string.format as
> follows:

Yes, and the same would apply to string concatenation, etc.

Actually, the concatenation works without tostring() as shown in my example, so userdata must have implemented the __concat metamethod.

But string.format("%s") obviously doesn't call the __tostring metamethod, which looks a bit illogical to me.
In Objective-C, NSLog(@"%@") will try to call the "description" method on the object.
See :

In PHP, this works too :
class toto { function __tostring() { return "ok"; } }
printf("%s", new toto); // prints "ok"
See :

I think Ruby uses %p which calls the inspect method.
Objective-C uses %@ for that.

Maybe Lua should add a new modifier to string.format() that would call the __tostring() metamethod in every case. 
Unless I overlooked it (in which case, sorry for the noise)...

Bertrand Mansion