Currently, to!int("1_234") results in an error, and
auto x = "1_234";
auto y = parse!int(x);
results in y == 1, x == "_234"
However, it would be nice if there was a way that literals in the D sense were parsed as well as straight C literals.
Liam McSherry created a PR to do this, but it was never fixed/accepted. Most of the discussion on this issue can be read in the PR comments here: https://github.com/D-Programming-Language/phobos/pull/3377
I believe what we need is a parseDInteger function that can be used as a basis for other functions.
Comment #1 by b2.temp — 2016-01-07T20:16:46Z
(In reply to Steven Schveighoffer from comment #0)
> Liam McSherry created a PR to do this, but it was never fixed/accepted. Most
> of the discussion on this issue can be read in the PR comments here:
> https://github.com/D-Programming-Language/phobos/pull/3377
The `AllowUnderscores` parameter from the original PR should rather be a template parameter. If someone wants to allow underscores, this is because he strongly expects that the function will be used with underscores (which is not so common with int literals).
That's stupid to create an additional "runtime branch" for such an option, so maybe only as a template parameter.
Comment #2 by schveiguy — 2016-01-07T20:32:50Z
The consensus seems to have been that parse isn't the right place to put this at all, but rather create a new function that deals with parsing D integers (which can have underscores).
TBH, I don't think the runtime variable was a forethought, just a way to modify the function so that it behaved like the original for existing calls. Sure, a template parameter would work better, but also remember that precedent has things like radix being a runtime parameter.
In any case, having a separate function solves the backwards compatibility problem, and is as effective as a template parameter. But I don't know whether it's worth a whole new function for it.
Runtime parameter, template parameter, or new function name, this functionality should be somewhere in the library IMO.
Comment #3 by dlang-bugzilla — 2016-01-08T03:04:28Z
*** Issue 20568 has been marked as a duplicate of this issue. ***
Comment #5 by matrix-magma-cloak — 2024-10-29T19:25:32Z
Just wanted to bring attention to this issue, since it hasn't been mentioned in a while and I ran into it yesterday. The lack of symmetry between the calls:
double d = to!double("1_000_000");
int i = to!int("1_000_000");
was surprising. I presume this wasn't the original intent, since it's good to avoid surprises in API.
Comment #6 by robert.schadek — 2024-12-01T16:25:43Z