rfc:number_format_separator
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:number_format_separator [2015/12/21 11:40] – Expand on RFC tpunt | rfc:number_format_separator [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2015-12-19 | * Date: 2015-12-19 | ||
* Author: Thomas Punt, tpunt@php.net | * Author: Thomas Punt, tpunt@php.net | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
+ | |||
===== Introduction ===== | ===== Introduction ===== | ||
- | Long numerical literals | + | Long numerical literals |
<code php> | <code php> | ||
- | 197823459; | + | // what number is this? |
- | 97802345932 > 97802349532; // which number is greater? | + | 197823459; |
- | 9803458239 === 9803457239; // are these numbers equal? | + | |
+ | // which number is greater? | ||
+ | 97802345932 > 97802349532; | ||
+ | |||
+ | // are these numbers equal? | ||
+ | 9803458239 === 9803457239; | ||
</ | </ | ||
- | Many languages have therefore introduced a digit separator | + | These are difficult |
<code php> | <code php> | ||
- | 197_823_459; | + | // what number is this? |
- | 97_802_345_932 > 97_802_349_532; // which number is greater? | + | 197_823_459; |
- | 9_803_458_239 === 9_803_457_239; // are these numbers equal? | + | |
+ | // which number is greater? | ||
+ | 97_802_345_932 > 97_802_349_532; | ||
+ | |||
+ | // are these numbers equal? | ||
+ | 9_803_458_239 === 9_803_457_239; | ||
</ | </ | ||
- | The readability greatly increases when using a digit separator, and so it is the intentions of this RFC to introduce this feature into PHP too. | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC seeks to add support for using separators | + | This RFC will add support for using the underscore character as a separator |
- | Quick example: | + | Example: |
<code php> | <code php> | ||
1_000_000; // versus 1000000 | 1_000_000; // versus 1000000 | ||
3.141_592; // versus 3.141592 | 3.141_592; // versus 3.141592 | ||
- | 0x025_612; // versus 0x025612 | + | 0x02_56_12; // versus 0x025612 |
0b0010_1101; | 0b0010_1101; | ||
+ | 0267_3432; // versus 02673432 | ||
+ | 1_123.456_7e2 // versus 1123.4567e2 | ||
</ | </ | ||
- | ==== Chosen Semantics ==== | + | The underscores will be stripped out during the lexing stage, and so the runtime will not be affected in any way. For example: |
+ | <code php> | ||
+ | var_dump(1_000_000); | ||
+ | </ | ||
- | The following semantics have been chosen: | + | ==== Chosen syntax ==== |
- | * Leading underscores are not allowed (conflicts with constant naming conventions) | + | |
- | * Trailing underscores are not allowed (not beneficial, decreases readability) | + | |
- | * Adjacent underscores are not allowed (not beneficial) | + | |
- | * Underscores are not allowed around the period for floats, around the **0x** for hexadecimal numbers, or around the **0b** for binary numbers (not beneficial, decreases readability) | + | |
- | Invalid usage examples: | + | The digit separator is used to mark boundaries between digits - it is not used to separate digits from other characters. The following syntax choices are therefore based on this. |
+ | |||
+ | === Disallow leading underscores === | ||
+ | |||
+ | Leading underscores will not enhance readability and will conflict with constant naming conventions. | ||
<code php> | <code php> | ||
- | _1; // interpreted as a constant | + | _100; // a valid constant |
+ | </ | ||
- | 1_; // PHP Parse error: | + | === Disallow trailing underscores === |
- | 1__000; // PHP Parse error: | + | Trailing underscores will not enhance readability - if anything, they will decrease it. |
+ | <code php> | ||
+ | 100_< | ||
+ | </ | ||
- | 100_.0; // PHP Parse error: | + | === Disallow adjacent underscores === |
- | 100._01; // PHP Parse error: | + | |
- | 0x_0123; // PHP Parse error: | + | Allowing for two or more underscores to be placed together will provide no further readability benefits. |
- | 0b_0101; // PHP Parse error: | + | <code php> |
+ | 1__000___000_000; // Parse error: syntax error, unexpected '__000___000_000' (T_STRING)... | ||
</ | </ | ||
- | Other than the aforementioned rules, | + | === Enable |
+ | |||
+ | Underscores are not allowed around the period for floats, around the **0x** for hexadecimal notation, around the **0b** for binary notation, or around the **e** for scientific notation. This is because readability will be negatively impacted, and it doesn' | ||
<code php> | <code php> | ||
- | 100_000; | + | 100_.0; // Parse error: |
- | 10_0.0_112; | + | 100._01; // Parse error: |
- | 0b01_10_11_00; | + | 0x_0123; // Parse error: |
+ | 0b_0101; // Parse error: | ||
+ | 1_e2; // Parse error: syntax error, unexpected ' | ||
+ | 1e_2; // Parse error: syntax error, unexpected ' | ||
</ | </ | ||
- | ==== Why the Underscore Character? ==== | + | === Enable for arbitrary grouping of digits === |
- | The underscore | + | |
+ | Underscores may be freely interspersed between arbitrary groups of digits, enabling for developers to group the digits as they see fit. One such argument for relaxing the interspersing of underscores is that not all countries group digits in sets of three [1]. | ||
+ | <code php> | ||
+ | 1_234_567; // grouped in sets of 3 | ||
+ | 123_4567; // grouped in sets of 4 (Japan, China) | ||
+ | 1_00_00_00_000; | ||
+ | |||
+ | 0x11_22_33_44_55_66; | ||
+ | 0x1122_3344_5566; | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Why the underscore character? ==== | ||
+ | |||
+ | The underscore: | ||
+ | * Is easy to type on the majority | ||
+ | * Does not conflict with PHP's grammar | ||
+ | * Acts as a clear and unambiguous delineator between digits (unlike the comma or period) | ||
- | The following | + | It has also been widely adopted as a digit separator in a number of other languages, including: |
* Ada | * Ada | ||
* D | * D | ||
Line 76: | Line 118: | ||
* Elixir | * Elixir | ||
- | Few other languages have deviated from using the underscore to separate digits. One notable | + | Few other languages have deviated from using the underscore to separate digits. One notable |
- | ==== Why no Support | + | ==== Why no support |
- | This RFC does not include stringy numerics because of the BC breakage involved. It will cause the coercion rules for strings to integers | + | This RFC does not include stringy numerics because of the BC breakage involved. It would mean changing |
+ | |||
+ | If formatting stringy | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 109: | Line 153: | ||
Support for stringy numerics could be added in the next major version. | Support for stringy numerics could be added in the next major version. | ||
- | ===== Proposed Voting Choices | + | ===== Vote ===== |
- | A simple yes/no voting option | + | A simple yes/no voting option |
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | Voting starts on January 13th and ends on January 20th. | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | A patch has been made - this part will be updated soon. | + | PR: https:// |
===== Implementation ===== | ===== Implementation ===== | ||
Line 122: | Line 173: | ||
===== References ===== | ===== References ===== | ||
+ | Current discussion: https:// | ||
+ | |||
Previous discussion on separators for numerical literals: https:// | Previous discussion on separators for numerical literals: https:// | ||
- | Current discussion: to be updated... | + | |
+ | [1]: http:// | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
Keep this updated with features that were discussed on the mail lists. | Keep this updated with features that were discussed on the mail lists. |
rfc/number_format_separator.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1