to but nothrow?

bauss jj_1337 at live.dk
Fri May 22 20:25:24 UTC 2020


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?

Ex. from C# there's int.TryParse etc.

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.


More information about the Digitalmars-d-learn mailing list