std.conv:to that does not throw?
Kyle Ingraham
kyle at kyleingraham.com
Thu Jan 30 01:38:07 UTC 2025
Does D have a 'try' `std.conv:to` that does not throw if it
fails? Something like:
```D
string input = "9";
int output;
auto parsed = input.tryTo!int(output);
```
`std.conv:to` is super flexible and does exactly what I need.
However, hitting an exception for conversion failures really
slows down my code. A conversion failure wouldn't be an exception
for my use case. I'm trying to write web application route
constraint code like what's [available in
C#](https://github.com/dotnet/aspnetcore/blob/main/src/Http/Routing/src/Constraints/IntRouteConstraint.cs#L53). There, code like `Int.TryParse` is used to figure out whether a string meets a route constraint.
`std.conv:to` is almost perfect with it's flexibility across
types. I'm just hoping I've missed the version that doesn't throw.
More information about the Digitalmars-d-learn
mailing list