Bug 11807 – Functions to attempt string-to-T conversions (a la C#'s TryParse)

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-12-23T11:40:00Z
Last change time
2013-12-24T02:35:27Z
Assigned to
nobody
Creator
peter.alexander.au

Comments

Comment #0 by peter.alexander.au — 2013-12-23T11:40:23Z
std.conv provides 'to' and 'parse', but both will throw exceptions if they fail to convert. The only way to test if a conversion is possible is to call these functions and catch the exception. This isn't a great situation because throwing exceptions is not good for performance. This enhancement requests something akin to C#'s TryParse, which returns a boolean false if the conversion failed and returns the converted value as an out parameter. Like this: bool tryParse(T)(string s, out T value) An alternative API would be to return a Nullable!T See Also: http://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx
Comment #1 by bearophile_hugs — 2013-12-23T14:10:38Z
See also Issue 6840
Comment #2 by peter.alexander.au — 2013-12-24T02:35:27Z
Duplicate of Issue 6840. *** This issue has been marked as a duplicate of issue 6840 ***