www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.conv:to that does not throw?

reply Kyle Ingraham <kyle kyleingraham.com> writes:
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 
https://github.com/dotnet/aspnetcore/blob/main/src/Http/Routing/src/Constraints/IntRout
Constraint.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.
Jan 29
next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
https://github.com/dotnet/aspnetcore/blob/main/src/Http/Routing/src/Constraints/IntRout
Constraint.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. It's been suggested before, but it's never been implemented (and would probably require quite a bit of refactoring if we want to avoid code duplication). It's definitely on the todo list for the next major version of Phobos, but we've never added such a function to the current version. So, if you want to avoid having the conversion throw, you have to test the value that you're converting first to make sure that the conversion won't fail - e.g. something like all!isDigit(input) if you're converting a string to an int. - Jonathan M Davis
Jan 29
parent Kyle Ingraham <kyle kyleingraham.com> writes:
On Thursday, 30 January 2025 at 02:08:49 UTC, Jonathan M Davis 
wrote:
 Unfortunately, there isn't currently a function like 
 std.conv.to which does not throw. It's been suggested before, 
 but it's never been implemented (and would probably require 
 quite a bit of refactoring if we want to avoid code 
 duplication). It's definitely on the todo list for the next 
 major version of Phobos, but we've never added such a function 
 to the current version.
Thanks very much Jonathan. That confirms what I thought (but really hoped wasn't the case). I'll take a look at what it might take to modify std.conv.to.
Jan 29
prev sibling next sibling parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Thursday, 30 January 2025 at 01:38:07 UTC, Kyle Ingraham 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 
https://github.com/dotnet/aspnetcore/blob/main/src/Http/Routing/src/Constraints/IntRout
Constraint.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.
its on my todo list for opend but isnt realisticly happening any time soon, I can suggest some patterns for setting up a cleaner `to` overloadset if you want to pick it up
Jan 29
parent reply Kyle Ingraham <kyle kyleingraham.com> writes:
On Thursday, 30 January 2025 at 02:52:49 UTC, monkyyy wrote:
 its on my todo list for opend but isnt realisticly happening 
 any time soon, I can suggest some patterns for setting up a 
 cleaner `to` overloadset if you want to pick it up
Much appreciated. I'd love some tips if you've got them.
Jan 29
parent monkyyy <crazymonkyyy gmail.com> writes:
On Thursday, 30 January 2025 at 03:01:43 UTC, Kyle Ingraham wrote:
 On Thursday, 30 January 2025 at 02:52:49 UTC, monkyyy wrote:
 its on my todo list for opend but isnt realisticly happening 
 any time soon, I can suggest some patterns for setting up a 
 cleaner `to` overloadset if you want to pick it up
Much appreciated. I'd love some tips if you've got them.
this is my understanding of how to make the overload set that covers the entirity of the d type system https://github.com/crazymonkyyy/debuglibprototype/blob/master/old/humanname.d in theory, youd go thru and implimet each thing and it would all just work
Jan 29
prev sibling next sibling parent "H. S. Teoh" <hsteoh qfbox.info> writes:
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
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"
Jan 29
prev sibling next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Wednesday, January 29, 2025 8:00:43 PM MST H. S. Teoh via
Digitalmars-d-learn wrote:
 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.
No, parse is for converting only the first part of the input, with the idea that you'll call it repeatedly to parse out multiple values, whereas to converts the entire string. parse will still throw. It's just not going to be under quite the same circumstances. And the fact that it parses what it can and leaves the rest behind to be parsed next can make for some surprising results sometimes, and you can easily end up with it converting less than you would have wanted, which can get rather interesting when it comes to reporting failures in a clean manner. parse has its uses, but personally, I prefer to split the input and then convert each piece individually rather than using parse. Either way, it's definitely not a nothrow version of to. It's its own thing with its own quirks. What we really need is something like tryTo that returns a Nullable which has no value when the conversion fails, and then preferably, tryTo would share its implementation with to (though that could get tricky, particularly if you want to minimize how deep the templates go - even more so given that to can't simply be a wrapper around tryTo if you want to provide any information about why it failed like it currently does). But to's implementation is probably going to get a major overhaul for Phobos v3 anyway to try to reduce how many template instantiations it incurs. Either way, tryTo is definitely planned as part of Phobos v3. - Jonathan M Davis
Jan 29
parent reply Kyle Ingraham <kyle kyleingraham.com> writes:
On Thursday, 30 January 2025 at 04:44:02 UTC, Jonathan M Davis 
wrote:
 What we really need is something like tryTo that returns a 
 Nullable which has no value when the conversion fails, and then 
 preferably, tryTo would share its implementation with to
I like a Nullable return much more than what I presented. I’ll have a look at how hard it might be to throw that together.
Jan 31
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Friday, January 31, 2025 3:45:55 PM MST Kyle Ingraham via
Digitalmars-d-learn wrote:
 On Thursday, 30 January 2025 at 04:44:02 UTC, Jonathan M Davis
 wrote:
 What we really need is something like tryTo that returns a
 Nullable which has no value when the conversion fails, and then
 preferably, tryTo would share its implementation with to
I like a Nullable return much more than what I presented. I’ll have a look at how hard it might be to throw that together.
If all you need is to convert strings to integers, then it's quite straightforward and shouldn't take long to write. The problem with regards to something like std.conv.to is that it does a _lot_ of different types of conversions. So, while we definitely should have something like tryTo in addition to having to, it's non-trivial due to how much to does. - Jonathan M Davis
Feb 01
prev sibling next sibling parent Paul Backus <snarwin gmail.com> writes:
On Thursday, 30 January 2025 at 01:38:07 UTC, Kyle Ingraham 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);
 ```
You could try something like this: ```d import std.typecons: Nullable, nullable; import std.conv: to, ConvException; Nullable!T tryTo(T, U)(U src) { try return nullable(src.to!T); catch (ConvException e) return Nullable!T.init; } unittest { assert("123".tryTo!int.get == 123); assert("hello".tryTo!int.empty); } ```
Jan 30
prev sibling parent reply Lance Bachmeier <no spam.net> writes:
On Thursday, 30 January 2025 at 01:38:07 UTC, Kyle Ingraham wrote:
 Does D have a 'try' `std.conv:to` that does not throw if it 
 fails?
Have you looked at [mir.conv](http://mir-core.libmir.org/mir_conv.html) and [mir.parse](http://mir-algorithm.libmir.org/mir_parse.html)?
Jan 30
parent reply Kyle Ingraham <kyle kyleingraham.com> writes:
On Thursday, 30 January 2025 at 17:19:19 UTC, Lance Bachmeier 
wrote:
 Have you looked at 
 [mir.conv](http://mir-core.libmir.org/mir_conv.html) and 
 [mir.parse](http://mir-algorithm.libmir.org/mir_parse.html)?
I’ll give those a try tonight. At first glance I see mention of throwing so they may not fit but I’ll know for certain soon enough.
Jan 31
parent reply Kyle Ingraham <kyle kyleingraham.com> writes:
On Friday, 31 January 2025 at 22:42:14 UTC, Kyle Ingraham wrote:
 On Thursday, 30 January 2025 at 17:19:19 UTC, Lance Bachmeier 
 wrote:
 Have you looked at 
 [mir.conv](http://mir-core.libmir.org/mir_conv.html) and 
 [mir.parse](http://mir-algorithm.libmir.org/mir_parse.html)?
I’ll give those a try tonight. At first glance I see mention of throwing so they may not fit but I’ll know for certain soon enough.
It turns out that they both do throw.
Jan 31
parent An Pham <home home.com> writes:
On Saturday, 1 February 2025 at 02:05:13 UTC, Kyle Ingraham wrote:
 On Friday, 31 January 2025 at 22:42:14 UTC, Kyle Ingraham wrote:
 On Thursday, 30 January 2025 at 17:19:19 UTC, Lance Bachmeier 
 wrote:
 Have you looked at 
 [mir.conv](http://mir-core.libmir.org/mir_conv.html) and 
 [mir.parse](http://mir-algorithm.libmir.org/mir_parse.html)?
I’ll give those a try tonight. At first glance I see mention of throwing so they may not fit but I’ll know for certain soon enough.
It turns out that they both do throw.
For integral types, you can try my implementation parseIntegral https://github.com/apz28/dlang/blob/main/source/pham/utl/utl_numeric_parser.d
Feb 01