lua-users home
lua-l archive

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


On 3/8/11, Bertrand Mansion <lua@mamasam.net> wrote:
> On Tue, Mar 8, 2011 at 1:23 PM, steve donovan
> <steve.j.donovan@gmail.com>wrote:
>
>> On Tue, Mar 8, 2011 at 1:56 PM, Michal Kottman <k0mpjut0r@gmail.com>
>> 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.

> 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)...
>

I didn't try string.format in my tests. I think you found another
Gotcha I should add to my documentation page. For the record, I did
implement the __tostring and __concat metamethods for NSString
userdata's. Plain old print() works with NSString userdata as well as
Cocoa APIs with the format strings such as NSLog(). LuaCocoa should
automatically handle either %@ or %s for NSString userdata.

Here is an example from my Gotcha section of my documentation:
local nsstring = NSString:alloc():initWithUTF8String_("Cocoa string")
NSLog("%@, %@, %@, %s", "lua string", true, 1.0, nsstring)

I haven't reviewed the string.format() implementation, but it seems to
be reasonable to expect __tostring to be invoked on userdata when the
%s token is found. Certainly as a user, I expect this.


-Eric
-- 
Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/