Thread overview
Does string.isNumeric mean that parse!int will not throw?
Feb 20, 2014
Cooler
Feb 20, 2014
Stanislav Blinov
Feb 20, 2014
Cooler
Feb 20, 2014
bearophile
Feb 20, 2014
Cooler
Feb 20, 2014
w0rp
Feb 20, 2014
Stanislav Blinov
Feb 20, 2014
w0rp
Feb 21, 2014
Jesse Phillips
February 20, 2014
The code:

string s = "...";
if(s.isNumeric){
    auto x = parse!int(s); // Can I be sure here that parse will
not throw?
}
February 20, 2014
On Thursday, 20 February 2014 at 19:11:55 UTC, Cooler wrote:
> The code:
>
> string s = "...";
> if(s.isNumeric){
>     auto x = parse!int(s); // Can I be sure here that parse will
> not throw?
> }

No. s may still contain data that is not convertible to int. For
example, "nan".
February 20, 2014
On Thursday, 20 February 2014 at 19:18:15 UTC, Stanislav Blinov
wrote:
> On Thursday, 20 February 2014 at 19:11:55 UTC, Cooler wrote:
>> The code:
>>
>> string s = "...";
>> if(s.isNumeric){
>>    auto x = parse!int(s); // Can I be sure here that parse will
>> not throw?
>> }
>
> No. s may still contain data that is not convertible to int. For
> example, "nan".

Is there any way to know that a string is convertible to a number
without throwing?
February 20, 2014
Cooler:

> Is there any way to know that a string is convertible to a number without throwing?

I suggested to add it:
https://d.puremagic.com/issues/show_bug.cgi?id=6840

Bye,
bearophile
February 20, 2014
On Thursday, 20 February 2014 at 19:23:28 UTC, Cooler wrote:
> On Thursday, 20 February 2014 at 19:18:15 UTC, Stanislav Blinov
> wrote:
>> On Thursday, 20 February 2014 at 19:11:55 UTC, Cooler wrote:
>>> The code:
>>>
>>> string s = "...";
>>> if(s.isNumeric){
>>>   auto x = parse!int(s); // Can I be sure here that parse will
>>> not throw?
>>> }
>>
>> No. s may still contain data that is not convertible to int. For
>> example, "nan".
>
> Is there any way to know that a string is convertible to a number
> without throwing?

You could do this.

import std.stdio;
import std.algorithm: all;
import std.ascii: isDigit;

void main(string[] args) {
     writeln("123".all!isDigit); // true
     writeln("12x".all!isDigit); // false
}

Combine the "all" algorithm which returns true if everything in a
range matches a predicate and use "isDigit" as the predicate
which returns true if the character is an ascii digit.

if (s.length > 0 && s.all!isDigit) {
     // Never throws now.
     auto x = parse!int(s);
}
February 20, 2014
On Thursday, 20 February 2014 at 19:46:35 UTC, w0rp wrote:

> if (s.length > 0 && s.all!isDigit) {
>      // Never throws now.
>      auto x = parse!int(s);
> }

And what about +/- and U/L suffixes? Or, say, different base (i.e. hexadecimal)?

It would be way more beneficial if Phobos' parse (or some additional function) returned some kind of Optional. Maybe a D implementation of Andrei's Expected<T>?
February 20, 2014
On Thursday, 20 February 2014 at 19:58:10 UTC, Stanislav Blinov
wrote:
> On Thursday, 20 February 2014 at 19:46:35 UTC, w0rp wrote:
>
>> if (s.length > 0 && s.all!isDigit) {
>>     // Never throws now.
>>     auto x = parse!int(s);
>> }
>
> And what about +/- and U/L suffixes? Or, say, different base (i.e. hexadecimal)?
>
> It would be way more beneficial if Phobos' parse (or some additional function) returned some kind of Optional. Maybe a D implementation of Andrei's Expected<T>?

True, this doesn't cover those. Returning a
Maybe/Option/Nullable/Whatever type from another function may be
better.
February 20, 2014
On Thursday, 20 February 2014 at 19:37:08 UTC, bearophile wrote:
> Cooler:
>
>> Is there any way to know that a string is convertible to a number without throwing?
>
> I suggested to add it:
> https://d.puremagic.com/issues/show_bug.cgi?id=6840
>
> Bye,
> bearophile

Yeap. Thanks. Will wait for the enhancment.
February 21, 2014
On Thursday, 20 February 2014 at 19:46:35 UTC, w0rp wrote:
> if (s.length > 0 && s.all!isDigit) {
>      // Never throws now.
>      auto x = parse!int(s);
> }

Nope, integer overflow exception. Though I didn't check if it would actually through that, but it should.