tryTo: non-throwing variant of std.conv.to

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Aug 15 10:38:23 UTC 2018


On Wednesday, August 15, 2018 3:21:29 AM MDT Per Nordlöw via Digitalmars-d-
learn wrote:
> Have anybody thought about non-throwing variants of std.conv.to
> typically named `tryTo`
> similar to what Folly
>
> https://github.com/facebook/folly/blob/master/folly/docs/Conv.md#non-throw
> ing-interfaces
>
> does, for instance, as
>
>      tryTo<int>(str).then([](int i) { use(i); });
>
> ?
>
> Would it be sane to add these to std.conv alongside existing
> std.conv.to so that underlying logic in std.conv.to can be reused?
>
> If so, AFAICT, existing std.conv.to should be implemented on top
> of std.conv.tryTo.

Yes. I've considered writing a set of conversion functions like std.conv.to
which return a Nullable!T (where the target type is T) and return
Nullable!T.init when the conversion fails rather than throwing, and they
really should be in std.conv alongside to, reusing logic as much as
reasonably possible, but it's not a small project. It's come up off and on
for a while now, so I expect that someone will do it eventually (and I may
at some point if no one else does), but obviously, someone has to actually
take the time to do it, or it's not going to happen.

- Jonathan M Davis






More information about the Digitalmars-d-learn mailing list