std.conv length=0

cc cc at nevernet.com
Wed Mar 28 20:03:36 PDT 2012


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.



More information about the Digitalmars-d-learn mailing list