www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - to but nothrow?

reply bauss <jj_1337 live.dk> writes:
Is there anyway to use the "to" template from std.conv but as 
nothrow?

Having it throw exceptions is not always acceptable because it's 
generally expensive.

Something that attempted to convert would be far more fesible.

Is there anything like that?



Those all return booleans for whether the conversion was 
successful or not.

Like what would the equivalent of this be:

```
if (int.TryParse("1234", out int number))
{
     // Use number ...
}
```

Sure I can do:

```
bool tryParse(From,To)(From fromValue, out To toValue)
{
     try
     {
         toValue = fromValue.to!To;
         return true;
     }
     catch (ConvException e)
     {
         toValue = To.init;
         return false;
     }
}
```

But that does not seem like an efficient approach.
May 22 2020
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, May 22, 2020 at 08:25:24PM +0000, bauss via Digitalmars-d-learn wrote:
 Is there anyway to use the "to" template from std.conv but as nothrow?
[...] There's std.conv.parse, though the interface is somewhat awkward, and it only works with character ranges. T -- I'm still trying to find a pun for "punishment"...
May 22 2020