On Wed, Jan 29, 2025 at 07:08:49PM -0700, Jonathan M Davis via Digitalmars-d-learn wrote: > On Wednesday, January 29, 2025 6:38:07 PM MST Kyle Ingraham via > Digitalmars-d-learn wrote: > > 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. > > Unfortunately, there isn't currently a function like std.conv.to which > does not throw.
I thought std.conv.parse* is supposed to fill that role? Or am I remembering wrong? They don't quite have the same level of convenience as .to, though. --T -- One reason that few people are aware there are programs running the internet is that they never crash in any significant way: the free software underlying the internet is reliable to the point of invisibility. -- Glyn Moody, from the article "Giving it all away"