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

Hex integer literals with decimal #1024

Open
jclark opened this issue Jan 18, 2022 · 5 comments
Open

Hex integer literals with decimal #1024

jclark opened this issue Jan 18, 2022 · 5 comments
Labels
Area/Lang Relates to the Ballerina language specification Type/Improvement Enhancement to language design

Comments

@jclark
Copy link
Collaborator

jclark commented Jan 18, 2022

At the moment:

  • decimal x = 0x1234; is OK, but
  • decimal:fromString("0x1234") gives an error

This is inconsistent: they should both be allowed or not allowed.

The spec also says that decimal:fromString("1234") should be an error, but that is clearly wrong (and jBallerina allows it).

@jclark jclark added this to the 2022R1 milestone Jan 18, 2022
@jclark
Copy link
Collaborator Author

jclark commented Jan 18, 2022

Note that decimal x = 0x1234.0; is not currently allowed.

@jclark
Copy link
Collaborator Author

jclark commented Jan 18, 2022

From an implementation perspective, I am not enthusiastic about allowing decimal:fromString("0x1234"). The standard decimal package decNumber doesn't provide support for it. So we would have to implement it manually using multiplications (at least for numbers out of range of int). The main point of decimal is efficient conversions from string.

We definitely don't want to support literals with hexadecimal exponents for decimal, which means we shouldn't support hexadecimal literals with a . for decimal.

@jclark
Copy link
Collaborator Author

jclark commented Jan 19, 2022

It does not seem consistent to me to allow decimal x = 0x1234; but not allow decimal x = 0x1234.0; But it's pointless work to allow the latter (which would also require hex exponents). So I think the right fix is

  • to disallow decimal x = 0x1234;
  • change spec to make decimal:fromString("1234") not an error i.e. allow DecimalNumber

@hasithaa Do you agree?

@jclark jclark modified the milestones: 2022R1, Swan Lake backlog Feb 1, 2022
@manuranga
Copy link
Contributor

Both int and float has fromHexString, why not for decimal ?

@jclark
Copy link
Collaborator Author

jclark commented Feb 3, 2022

@manuranga Previous comments explain that.

@anupama-pathirage anupama-pathirage added Type/Improvement Enhancement to language design Area/Lang Relates to the Ballerina language specification labels Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/Lang Relates to the Ballerina language specification Type/Improvement Enhancement to language design
Projects
None yet
Development

No branches or pull requests

3 participants