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

Re: another take on hackers and painters




> Date: Thu, 22 May 2003 14:12:35 -0700
> From: Paul Prescod <paul@prescod.net>
> 
> I think that the real problem reported by (ex-) Perl users is that Perl 
> sometimes sees string values that do not have reasonable integer 
> representations and rather than telling you that it just guesses that 
> you meant for the value to be 0. Often that will mask error conditions 
> (not just potential logic bugs, but errors that have been triggered). I 
> have a hard time believing that this is the behaviour you want often 
> enough for it to be the default. For the rare cases where you want 
> "convert the number if it is a number otherwise give me 0" I'd rather 
> say so explicitly. 0 is not necessarily the best fallback value anyhow.
> 

It seems to me that the Right Thing, given that you want string->number
autoconversion, is to try to do the conversion and raise an exception if
the string cannot be reasonably converted to a number.  Why doesn't perl do
that?  In python:

>>> int("100")
100
>>> int("foobar")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): foobar

Of course, this isn't autoconversion, but something like this could be used
for autoconversion.

Actually, I've gotten frustrated many times because C's atoi() fails
silently as well.  Of course C doesn't have exceptions...

Mike