lua-users home
lua-l archive

Coercion to string does not call metamethod (Was: Lua minus null (-0)

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


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
 

AltStyle によって変換されたページ (->オリジナル) /