google / jsonnet

Jsonnet - The data templating language
http://jsonnet.org
Apache License 2.0
6.87k stars 436 forks source link

Support underscores or other separators in numeric literals. #1155

Open jeschkies opened 3 months ago

jeschkies commented 3 months ago

It would be nice to have support for underscores in numeric literals to be able to write 500_000_000 instead of 500000000. This is a common feature for digit grouping in languages. See e.g. PEP-0515.

seizethedave commented 1 month 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?

sparkprime commented 1 month ago

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?

jeschkies commented 1 month ago

what is the difference between PEP-0515 and the ECMAScript approach?

ES6 and Python both allow _ as a separator.

seizethedave commented 1 month ago

ECMA proposal, PEP515.

Differences between the two seem to be:

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 _...

seizethedave commented 1 month ago

Since that's the only difference I can find, what if we just followed ECMAScript? To put it into words:

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