Open jeschkies opened 6 months ago
This would be valuable. Tons of big numeric constants embedded in jsonnet out there.
This would be a deviation from json, but ECMAScript has digit separators. Would the mods look favorable upon this addition?
SGTM but there's quite a diversity in approaches across languages. Jsonnet typically tries to be compatible with Python or ECMAScript so what is the difference between PEP-0515 and the ECMAScript approach?
what is the difference between PEP-0515 and the ECMAScript approach?
ES6 and Python both allow _
as a separator.
Differences between the two seem to be:
int('123_456')
at runtime. ECMAScript doesn't.Actually, I'll have to take a closer look later, but among the subset of ECMAScript things that Jsonnet supports (e.g., no octals), that's the only difference I've noticed so far.
The big thing they both have in common is that each underscore can appear between two numerals (0-9), and nothing else can be touching the underscore. Not an e
, a .
, not another _
...
Since that's the only difference I can find, what if we just followed ECMAScript? To put it into words:
_
character.1_000
, 123_456_789
1_000.123_456
, 3.141_592_653
1.23e4_567
, 1_000e1_000
1_1
, 1_1_1_1_1_1
, and 1_11_111_1111
are all valid literals.)_
may appear between any two numeric digits._
may not appear next to any of the other non-digit characters encountered in numeric literals. (Including another _
.)
1_000_.0
, 1._123
, 7_e3
, 7e_3
, 7e-_3
, 7e+_3
, 1__000
_
may not appear a the beginning or end of a numeric literal.
123_
, _123
. (The latter would be lexed as a variable name.)1_000
and 1000
will have an identical representation in the parse tree and other Jsonnet internals.std.parseInt
.Happy to put this in a doc or something more amenable to commenting. I'm also keen to submit a PR. I've already drafted one in a fork.
David
It would be nice to have support for underscores in numeric literals to be able to write
500_000_000
instead of500000000
. This is a common feature for digit grouping in languages. See e.g. PEP-0515.