-
Notifications
You must be signed in to change notification settings - Fork 3k
Allow underscores in numeric literals #2324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
To make long integer literals more readable. Examples: * 123_456_789 * 123_456.789_123 * 16#123_ABC
I welcome this in principle. The digit separator feature has been around since at least Ada83, and is currently present in about 7 or 8 additional languages according to a proposal to add them to "Go". I would suggest to not allow $_ wherever a digit is expected, but only as a separator between digits (the Ada rule). See https://go.googlesource.com/proposal/+/master/design/19308-number-literals.md section "Digit Separators" for some background. |
@mikpe I also think it doesn't makes much sense to support this in base. In current implementation you can have
So, only allow it if chars left and right from With current implementation all of this should work, but doesn't make much sense:
Leading underscore |
Yes, leading, trailing, or repeated underscores should not be allowed. That's what's meant by being a separator between groups of digits. In Erlang the base is simply a decimal numeral, and I would prefer to allow underscores there for consistency -- I don't think it causes any problems. |
@mikpe I updated my implementation according to your comments (as a separate commit for now). File that I was using to test it:
In short: it's allowed as long as there is a digit from both sides of |
@@ -940,13 +940,16 @@ escape_char(C) -> C. | |||
|
|||
scan_number([C|Cs], St, Line, Col, Toks, Ncs) when ?DIGIT(C) -> | |||
scan_number(Cs, St, Line, Col, Toks, [C|Ncs]); | |||
scan_number([$_,Next|Cs], St, Line, Col, Toks, [Prev|_]=Ncs) when | |||
?DIGIT(Next) andalso ?DIGIT(Prev) -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be more efficient to rewrite (here and in other cases) as
scan_number([Pre, $_, Post | Cs], ...) when ?DIGIT(Pre), ?DIGIT(Post) ->
scan_number(..., [Post, $_, Pre | Ncs]);
and put as 1st function clause.
Should I?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that would pessimize the common case (no $_
), while not really optimizing the $_
case. The current code looks fine to me.
I guess it makes sense to update documentation as well. At least here: http://erlang.org/doc/reference_manual/data_types.html#number |
Remove underscores from numbers only when necessary.
Avoid some memory allocation when scanning based integers. (This has nothing to do with the new underscore-in-integers feature.)
And add a few more tests.
We've had a Technical Board (TB) meeting discussing your PR, and the Since it's a language extension, we think there should be an EEP (see We agree with @mikpe: underscore is to be allowed between digits (as a The underscores are to be lost when representing abstract forms or As you suggest, the Erlang Reference Manuals needs to be updated The Erlang mode for Emacs needs to be updated. The Erlang Debugger uses wxWidgets, which in turn uses Scintilla. It Beside these general comments from the TB, I've looked at your code |
Great news, thanks!
Oh, I will try, yes.
Will do
The only place I found in erlang.el is: Lines 1227 to 1230 in a62aed8
I guess it's responsible for highlighting Should I care about
a-ha! That's why I was not able to use |
I have to admit that I don't know much about the Emacs mode. If you |
EEP was submitter to mailing list as well as in a form of PR to eep github repository. |
I updated the docs and emacs erlang-mode. Is there anything I can do now? |
5d5d043
to
9d545d4
Compare
I think it looks good. I cannot make the emacs mode work. In fact we (I and Dan) cannot see The examples in the docs are not colored correctly. I've been My take on this PR is that we can manage without updated highlightings Thank you for your effort! |
regarding highlightjs, yep, it would be relatively easy to fix in the upstream: but then the version of the lib that is bundled needs to be updated in Erlang as well like it was done here 5e30a83 but it will be a separate PR i guess |
I'll take of it. Again, thanks for your effort! |
Just draft to get some feedback. Can update docs, erlang-mode.el and tests if we are ok to proceed.
This is to make long numeric literals more readable. The idea is borrowed from Rust language (rust spec)
Examples:
123_456_789
vs123456789
123_456.789_123
vs123456.789123
16#123_ABC
vs16#123ABC
2#1100_0011_0101
vs2#110000110101
One minor problem with current implementation is thaterl_scan:string("123_456", text)
will return{integer,[{text,"123456"},<..>],123456}
- so, text will be "123456" and not original "123_456", but this can be fixed, if it's important.