[erlang-bugs] string:to_float/1 and string:to_integer/1

Robert Virding rvirding@REDACTED
Tue May 6 00:00:16 CEST 2008


The two functions, to_float/1 and to_integer/1, in string are seriously
broken as they don't raise an error on wrong type of input data but return
an error value. This is inconsistent with the rest of the string module and
most other module in the standard libraries. The norm is that bad input data
is considered an error. I know it is documented but that just makes it a
documented bug. They should be either changed or removed. Or the rest of
string changed to be consistent.

I personally think that these functions anyway should not be in string, io
or io_lib is a better place for them. Should there be a string:to_atom which
parses its input string and returns the atom which may be at the beginning
of the string. Sorry for being sarcastic they *are* very weird functions.

I would also debate the error return if there is no integer or float, the
user should know what they are doing. Also returning an {error,E} tuple and
not an {ok,I,Rest} tuple is completely inconsistent, both should be tagged.
As it now you cannot just matched against the success case:

{Int,Rest} = string:to_integer(String)

but must use a case as both tuples are the same size and only one is tagged.
This also breaks a common idiom.

In all these functions cannot be considered as anything other than mis
formed bugs.

Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-bugs/attachments/20080506/1af638c6/attachment.htm>


More information about the erlang-bugs mailing list