Jump to page: 1 2
Thread overview
std.conv:to that does not throw?
Jan 30
monkyyy
Jan 30
monkyyy
Feb 01
An Pham
January 30

Does D have a 'try' std.conv:to that does not throw if it fails? Something like:

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#. 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.

January 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
> 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. 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



January 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? Something like:

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#. 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

January 30
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.
January 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:
> > 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"
January 30

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.

January 30

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

January 29
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



January 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? Something like:

string input = "9";
int output;
auto parsed = input.tryTo!int(output);

You could try something like this:

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);
}
January 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 and mir.parse?

« First   ‹ Prev
1 2