www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.conv length=0

reply Derek Parnell <derek nomail.afraid.org> writes:
Currently, the functions in std.conv throw an exception if the input string
is empty. What is the rationale for this? 

I find myself writing shims like ...

 int makeInt(char[] d)
 {
    if (d.length == 0) return toInt("0");
    return toInt(d);
 }

Is there anyone here that relies on an empty string throwing a conversion
error?

Also, I see that there is no link back to the DWiki/DocComments in the
std.conv docs.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Justice for David Hicks!"
10/04/2007 5:27:01 PM
Apr 10 2007
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Derek Parnell wrote:
 Currently, the functions in std.conv throw an exception if the input string
 is empty. What is the rationale for this? 
 
 I find myself writing shims like ...
 
  int makeInt(char[] d)
  {
     if (d.length == 0) return toInt("0");
     return toInt(d);
  }
 
 Is there anyone here that relies on an empty string throwing a conversion
 error?

Well the empty string is not a number, so it seems clear that there are times (perhaps the majority even) when you would want to treat that as an ill-formatted number error. Just like "fred" isn't a number. I'm assuming that throws an exception too. --bb
Apr 10 2007