Numeric Separators Update
goto@google.com
Recap: numeric separators - stage 3
// Allows including “_” between numbers for readability�//�// - no runtime semantics, ignored�// - only single separator exclusively between numbers �// e.g. _1, 1_ and 1__2 not allowed as numeric separators�// �// Example:
let budget = 1_000_000_000_000; // decimals�let nibbles = 0b1010_0001_1000_0101; // binary�let message = 0xA0_B0_C0; // hex
Recap: numeric literals extensions - stage 1
// Generalizes bigint built-in, e.g. 1236536253453n�//�// Semantics: 1234_i => �// _i(Object.freeze({string: "1234", number: 1234}))�//�// Examples:
4525_i // imaginary numbers�300_px // pixels�235435.461_m // meters
Issue
0x123_abc
Is this a numeric separator or an extended literal?
Alternative 1: different sigil, compatible with resolution mechanism
// two __ , e.g. 0x12_34_56__bytes
NumericLiteral __m
�Pros: works with the resolution mechanism of extensions (identifiers).�Cons: not as ergonomic?
Alternative 2: different sigil, incompatible with current resolution
// Anything after the ` is taken as the identifier for the extension
// e.g. 0x12_34_ab`bytes, other ideas: ~, $, @, spaces
NumericLiteral `cm��Pros: more ergonomic�Cons: incompatible with the current resolution mechanism (which rely on everything following the literal to be an identifier)
Alternate 3: Restrict Extensions to Decimals
// Only applies to decimals, disallowed in other numeric literals.
// e.g. 0x12_AB (separator), 123_px (extension), or 1_000_px (both)
DecimalLiteral _px
Pros: desirable/original syntax for extensions.�Cons: inconsistency, unavailable outside decimals.
Alternate 4: Couple separators and extensions
Numeric Literal Separator champions couple the Numeric Literal Extension proposal and work on making them harmonious. We accept the consequence of additional labor, as well as Stage 3 -> 2 setback.��E.g. use c++’s notation for numeric separators, 1'000'000'000_px; (See annex)
Alternative | Description | Example | Pros | Cons |
Extensions: __�Separators: _ | Different sigil for extensions, Same Resolution | 12__px 1_000_000 | Compatible with both proposals | Ergonomics of extensions |
Extension: ' Separators: _ | Different sigil for extensions, Different Resolution | 12'px, 12 px, 12'px 1_000_000 | Ergonomics of extensions | Incompatible with current resolution mechanism |
Extensions: _ Separators: _ | Restrict to decimal extensions | 12_px | Compatible with both proposals | Inconsistency between numeric literals (e.g. 0x12_AB) |
Extensions: _ Separators: ' | Different sigil for numeric separators | 12_px 1’000’000 | Compatible with both proposals | Inconsistency with other languages, ergonomics of separators |
Alternative 1?
ANNEX
Notable Background
C++ uses ` for separators and _ for extensions.
E.g. 0x123`abc.
Con:
Alternative #2: candidates for extensions
Pick a different sigil:
- __, e.g. 0x123__abc� - space, e.g. 0x123 px� - $, e.g. 0x123$abc
Limit to decimals only: