digitalmars.D.learn - std.conv:to that does not throw?
- Kyle Ingraham (15/15) Jan 29 Does D have a 'try' `std.conv:to` that does not throw if it
- Jonathan M Davis (11/26) Jan 29 Unfortunately, there isn't currently a function like std.conv.to which d...
- Kyle Ingraham (5/12) Jan 29 Thanks very much Jonathan. That confirms what I thought (but
- monkyyy (4/20) Jan 29 its on my todo list for opend but isnt realisticly happening any
- Kyle Ingraham (2/5) Jan 29 Much appreciated. I'd love some tips if you've got them.
- monkyyy (6/11) Jan 29 this is my understanding of how to make the overload set that
- H. S. Teoh (7/30) Jan 29 I thought std.conv.parse* is supposed to fill that role? Or am I
- Jonathan M Davis (22/27) Jan 29 No, parse is for converting only the first part of the input, with the i...
- Kyle Ingraham (4/7) Jan 31 I like a Nullable return much more than what I presented. I’ll
- Jonathan M Davis (7/14) Feb 01 If all you need is to convert strings to integers, then it's quite
- Paul Backus (18/25) Jan 30 You could try something like this:
- Lance Bachmeier (4/6) Jan 30 Have you looked at
- Kyle Ingraham (5/8) Jan 31 I’ll give those a try tonight. At first glance I see mention of
- Kyle Ingraham (2/10) Jan 31 It turns out that they both do throw.
- An Pham (4/15) Feb 01 For integral types, you can try my implementation
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
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
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
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
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 upMuch appreciated. I'd love some tips if you've got them.
Jan 29
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: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 workits 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 upMuch appreciated. I'd love some tips if you've got them.
Jan 29
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: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"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.
Jan 29
On Wednesday, January 29, 2025 8:00:43 PM MST H. S. Teoh via Digitalmars-d-learn wrote: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 DavisUnfortunately, 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.
Jan 29
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 toI 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
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: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 DavisWhat 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 toI 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.
Feb 01
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
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
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
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:It turns out that they both do throw.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
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:For integral types, you can try my implementation parseIntegral https://github.com/apz28/dlang/blob/main/source/pham/utl/utl_numeric_parser.dOn Thursday, 30 January 2025 at 17:19:19 UTC, Lance Bachmeier wrote:It turns out that they both do throw.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.
Feb 01