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 reply 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
parent reply "cc" <cc nevernet.com> writes:
On Tuesday, 10 April 2007 at 07:46:35 UTC, Bill Baxter wrote:
 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 too thought this behavior was silly, so I wrote a little wrapper for it (replying 5 years after the fact because someone'll probably stumble onto this question through Google like I did): import std.conv : toOrig = to; T toSafe(T, S)(S arg) { static if ((is(T == int) || (is(T == real))) && is(S == string)) if (!arg.length) return 0; return toOrig!T(arg); } alias toSafe to; and then just continue to call 'to' normally.
Mar 28 2012
parent reply James Miller <james aatch.net> writes:
On 29 March 2012 16:03, cc <cc nevernet.com> wrote:
 On Tuesday, 10 April 2007 at 07:46:35 UTC, Bill Baxter wrote:
 I too thought this behavior was silly, so I wrote a little wrapper for it
 (replying 5 years after the fact because someone'll probably stumble onto
 this question through Google like I did):
I award thee the "Necromancer" badge, for reviving a long-dead thread. -- James Miller
Mar 28 2012
parent reply "Jesse Phillips" <jessekphillips+D gmail.com> writes:
On Thursday, 29 March 2012 at 03:40:55 UTC, James Miller wrote:

 I award thee the "Necromancer" badge, for reviving a long-dead 
 thread.

 --
 James Miller
I find the distaste of reviving a thread strange. It would be like removing the "reopened" feature of bug tracking software (who wants to transpose all that information).
Mar 28 2012
parent reply James Miller <james aatch.net> writes:
On 29 March 2012 17:05, Jesse Phillips <jessekphillips+D gmail.com> wrote:
 On Thursday, 29 March 2012 at 03:40:55 UTC, James Miller wrote:

 I award thee the "Necromancer" badge, for reviving a long-dead thread.

 --
 James Miller
I find the distaste of reviving a thread strange. It would be like removing the "reopened" feature of bug tracking software (who wants to transpose all that information).
I have no problem with it, if I did, I would have said so. -- James Miller
Mar 28 2012
parent "Jesse Phillips" <Jessekphillips+D gmail.com> writes:
On Thursday, 29 March 2012 at 04:25:54 UTC, James Miller wrote:
 I find the distaste of reviving a thread strange. It would be 
 like removing
 the "reopened" feature of bug tracking software (who wants to 
 transpose all
 that information).
I have no problem with it, if I did, I would have said so. -- James Miller
I realized that, but I see those that revive threads all the time say, "sorry for the old thread" And I think there have been moderator complaints on some phpbb forums I've been (maybe the rules of conduct).
Mar 29 2012