lua-users home
lua-l archive

Re: String-to-number coercion

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


On 9/15/2013 5:26 PM, Luiz Henrique de Figueiredo wrote:
2013年9月10日 Andrew Starks <andrew.starks@trms.com>:
In my very humble opinion (and despite my own transgressions into
discussions on its redesign :), it gets this balance perfectly... Except
when it prompts strings to numbers. .)
More precisely: the exact spot where this irks me is that if you set
metamethods for str:__add etc, they don't get invoked when tonumber()
succeeds for both operands, EVEN WHEN BOTH ARE STRINGS.
Wthat do you mean, exactly? Can you please give an example?
debug.setmetatable("",{
__add=function()
error("can't add strings, you know") end})
print("1"+"2")
this prints 3 rather than raising an error.
change the last line to print("1"+"ZZ")
and the error in the above metatable is raised, rather than the standard "attempt to perform arithmetic on a string value".

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