Skip to content
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

Consecutive underscores in number literals and number parsing methods #11203

Closed
straight-shoota opened this issue Sep 12, 2021 · 2 comments · Fixed by #11211
Closed

Consecutive underscores in number literals and number parsing methods #11203

straight-shoota opened this issue Sep 12, 2021 · 2 comments · Fixed by #11211

Comments

@straight-shoota
Copy link
Member

straight-shoota commented Sep 12, 2021

Both integer literals and number parsing methods like String#to_i (with underscore: true) allow and ignore underscores in the number:

1_1                            # => 11
"1_1".to_i(underscore: true)   # => 11

The semantics are very similar but there is a difference: Literals allow multiple consecutive underscores, which is an error in String#to_i:

1__1                          # => 11
"1__1".to_i(underscore: true) # Error: Invalid Int32: 1__1

Considering the similarity of both approaches, it should be expected that both underscore semantics are identical and don't differ by such a minor detail.
(It would also help for using parsing methods in the compiler implementation.)

Originally brought up in #11196 (comment) ff.

@HertzDevil
Copy link
Contributor

1__1 is a syntax error in both Ruby and Python, and in C++ where the separator is ' instead, 1''1 is also an error there.

@oprypin
Copy link
Member

oprypin commented Nov 30, 2021

I think consecutive underscores should be disallowed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants