lua-users home
lua-l archive

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


On Jun 9, 2011 4:54 AM, "Dirk Laurie" <dpl@sun.ac.za> wrote:
>
> On Thu, Jun 09, 2011 at 12:15:37PM +0200, David Kastrup wrote:
> >
> > lua
> > Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> > > debug.setmetatable(0, {__tostring =
> > >>     function(n) local s=string.format("%.14g",n)
> > >>     if s=='-0' then s='0' end return s
> > >>     end})
> > > print(-0)
> > 0
> > > print("Oh"..-0)
> > Oh-0
>
> Very interesting!  We're back to that other old favourite, implicit
> conversion for concatenation, which bypasses the metatable.  It's not
> the fault of debug.setmetatable, you also get it with the non-debug one:
>
> > t={}
> > setmetatable(t, {__tostring = function(self) return "table t" end})
> > =t
> table t
> > ="show "..t
> stdin:1: attempt to concatenate global 't' (a table value)
> stack traceback:
>    stdin:1: in main chunk
>    [C]: in ?
>
> Is that a bug?
>
> Dirk
>
>

IIRC the only time __tostring is called is by tostring(). I've been annoyed several times that some other functions such as string.format and lua_tostring don't call it. I'd consider it a bug, but the designers may disagree...

--
Sent from my toaster.