lua-users home
lua-l archive

Re: Arithmetic on strings

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


> It seems to be impossible to override arithmetic operators on
> strings. One can for example set the __add metamethod, but
> it only gets called when at least one of the strings cannot be
> coerced to a number. For example:
> 
> getmetatable''.__add = function(x,y) return x..y end
> 'a'+'b' --> 'ab', fine
> '1'+'2' --> 3
In general, arithmetic metamethods are only called when Lua "does not
know what to do". Lua "does know what to do" with '1' + '2'.
(Probably the right thing to do would be to elliminate coersion from
strings to numbers altogether...)
-- Roberto

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