lua-users home
lua-l archive

Re: Is string always same as number? (the consistency of lua)

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


On Fri, Feb 11, 2011 at 05:51:58PM +0200, Greg Falcon wrote:
> On Fri, Feb 11, 2011 at 8:23 AM, Dirk Laurie <dpl@sun.ac.za> wrote:
> > I ask you to grant that:
> >    1. People who do not use the feature are not in any way affected
> >        by whether it is available or not.
> 
> That's only true if their code has no bugs. If someone accidentally
> tries to add two strings together (maybe by using + where they mean
> ..), there's a chance it does an unexpected thing at runtime, rather
> than just raising an error.
> 
On the other hand, if the use of 'tonumber' is compulsory, some
errors give less informative messages. Example: in the following 
code, y is supposed to be a string that can be coerced to a number.
Some bug has caused y to be invalid. 
If I rely on implicit conversion, I get:
> z=x+y
stdin:1: attempt to perform arithmetic on global 'y' (a string value)
If I avoid implicit conversion, whether because I abhor it or because
the thought police have taken it away from me:
> z=x+tonumber(y)
stdin:1: attempt to perform arithmetic on a nil value
I don't know whether x or y is causing the error, and if it is y,
I don't know its type.
Dirk

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