[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Arithmetic on strings
- From: "M. Edward (Ed) Borasky" <znmeb@...>
- Date: Fri, 6 Jul 2012 21:46:36 -0700
I'm new enough to Lua that doing that wouldn't break any of my code.
But if something is in a language, someone will use it. ;-)
On Fri, Jul 6, 2012 at 9:06 PM, Tim Hill <drtimhill@gmail.com> wrote:
> I'd vote for removing string coercion! Anyone else?
>
> On Jun 13, 2012, at 6:57 AM, Roberto Ierusalimschy wrote:
>
>>> 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
>>
>
>
--
Twitter: http://twitter.com/znmeb Computational Journalism Server
http://j.mp/compjournoserver
Data is the new coal - abundant, dirty and difficult to mine.