[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Coercion to string does not call metamethod (Was: Lua minus null (-0)
- From: Dirk Laurie <dpl@...>
- Date: Thu, 9 Jun 2011 12:53:12 +0200
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
- References:
- Re: Lua minus null (-0), Dirk Laurie
- Re: Lua minus null (-0), Henk Boom
- Re: Lua minus null (-0), Dirk Laurie
- Re: Lua minus null (-0), Everett L Williams II
- Re: Lua minus null (-0), Dirk Laurie
- Re: Lua minus null (-0), David Kastrup
- Re: Lua minus null (-0), Dirk Laurie
- Re: Lua minus null (-0), David Kastrup
- Re: Lua minus null (-0), Dirk Laurie
- Re: Lua minus null (-0), David Kastrup