digitalmars.D.learn - tryTo: non-throwing variant of std.conv.to
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (11/11) Aug 15 2018 Have anybody thought about non-throwing variants of std.conv.to
- Seb (11/23) Aug 15 2018 Well, for now you can use `ifThrown` from std.exception:
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (6/12) Aug 15 2018 But the whole idea is to avoid both throwing and catching
- Jonathan M Davis (11/23) Aug 15 2018 Yes. I've considered writing a set of conversion functions like std.conv...
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (4/9) Aug 15 2018 Put something together to get early feedback on the idea:
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-throwing-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.
Aug 15 2018
On Wednesday, 15 August 2018 at 09:21:29 UTC, Per Nordlöw 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-throwing-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.Well, for now you can use `ifThrown` from std.exception: https://dlang.org/phobos/std_exception.html#ifThrown --- "foo".to!int.ifThrown(42) --- https://run.dlang.io/is/nlZKOw Usage of optionals/nullables is sadly not very common in Phobos and I think if we want everything to work nicely, we probably have to start a new standard library (or do massive refactorings of the existing one.)
Aug 15 2018
On Wednesday, 15 August 2018 at 09:26:26 UTC, Seb wrote:But the whole idea is to avoid both throwing and catching exceptions at all. As this results in orders of magnitudes of drop in performance for the throwing case. That's the reason why Folly has this aswell. Thanks anyway, Seb.If so, AFAICT, existing std.conv.to should be implemented on top of std.conv.tryTo.Well, for now you can use `ifThrown` from std.exception: https://dlang.org/phobos/std_exception.html#ifThrown --- "foo".to!int.ifThrown(42)
Aug 15 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
Aug 15 2018
On Wednesday, 15 August 2018 at 10:38:23 UTC, Jonathan M Davis wrote:Put something together to get early feedback on the idea: https://github.com/dlang/phobos/pull/6665Would 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
Aug 15 2018