rfc:number_format_separator

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:number_format_separator [2015/12/23 21:54] tpuntrfc: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: Draft+  * Status: Declined
   * First Published at: http://wiki.php.net/rfc/number_format_separator   * First Published at: http://wiki.php.net/rfc/number_format_separator
  
Line 10: Line 10:
 Long numerical literals can be a source of poor readability in code. Take the following examples: Long numerical literals can be a source of poor readability in code. Take the following examples:
 <code php> <code php>
-197823459; // what number is this? +// 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;
 </code> </code>
  
 These are difficult to read and difficult reason about. To ameliorate this issue, this RFC proposes the introduction of a digit separator in numerical literals. This will enable the examples above to be rewritten as: These are difficult to read and difficult reason about. To ameliorate this issue, this RFC proposes the introduction of a digit separator in numerical literals. This will enable the examples above to be rewritten as:
 <code php> <code php>
-197_823_459; // what number is this? +// 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;
 </code> </code>
  
Line 33: Line 43:
 0b0010_1101; // versus 0b00101101 0b0010_1101; // versus 0b00101101
 0267_3432; // versus 02673432 0267_3432; // versus 02673432
 +1_123.456_7e2 // versus 1123.4567e2
 </code> </code>
  
-==== 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); // int(1000000) 
 +</code> 
 + 
 +==== Chosen syntax ==== 
 + 
 +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 ===+=== Disallow leading underscores ===
  
 Leading underscores will not enhance readability and will conflict with constant naming conventions. Leading underscores will not enhance readability and will conflict with constant naming conventions.
Line 44: Line 62:
 </code> </code>
  
-=== Disallow Trailing Underscores ===+=== Disallow trailing underscores ===
  
 Trailing underscores will not enhance readability - if anything, they will decrease it. Trailing underscores will not enhance readability - if anything, they will decrease it.
Line 51: Line 69:
 </code> </code>
  
-=== Disallow Adjacent Underscores ===+=== Disallow adjacent underscores ===
  
 Allowing for two or more underscores to be placed together will provide no further readability benefits. Allowing for two or more underscores to be placed together will provide no further readability benefits.
Line 58: Line 76:
 </code> </code>
  
-=== Enable Underscores Between Digits Only ===+=== Enable underscores between digits only ===
  
 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't really serve the purpose of a "digit separator." 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't really serve the purpose of a "digit separator."
Line 70: Line 88:
 </code> </code>
  
-=== Enable for Arbitrary Grouping of Digits ===+=== Enable for arbitrary grouping of digits ===
  
 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]. 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].
Line 79: Line 97:
  
 0x11_22_33_44_55_66; // a number to be used as bytes, grouped by bytes 0x11_22_33_44_55_66; // a number to be used as bytes, grouped by bytes
- 
 0x1122_3344_5566; // a number to be used as 16-bit data, grouped by word 0x1122_3344_5566; // a number to be used as 16-bit data, grouped by word
 </code> </code>
  
  
-==== Why the Underscore Character? ====+==== Why the underscore character? ====
  
 The underscore: The underscore:
Line 103: Line 120:
 Few other languages have deviated from using the underscore to separate digits. One notable exception is C++, where it could not use an underscore because of conflicts with user-defined literals (specifically in a hexadecimal context). Because PHP does not have such user-defined literals, there are no technical problems with using the underscore as a digit separator. This proposal therefore seeks to follow suite with the other languages. Few other languages have deviated from using the underscore to separate digits. One notable exception is C++, where it could not use an underscore because of conflicts with user-defined literals (specifically in a hexadecimal context). Because PHP does not have such user-defined literals, there are no technical problems with using the underscore as a digit separator. This proposal therefore seeks to follow suite with the other languages.
  
-==== Why no Support for Stringy Numerics? ==== +==== Why no support for stringy numerics? ==== 
-This RFC does not include stringy numerics because of the BC breakage involved. It would cause the coercion rules for strings to integers to change, which may potentially have wide-ranging impacts for PHP programs. If formatting stringy integer literals is desired, then support for these can be done in the next major version of PHP.+This RFC does not include stringy numerics because of the BC breakage involved. It would mean changing the coercion rules for strings to integers, which may potentially have wide-ranging impacts for PHP programs. Also, support for stringy numerics can be quite easily emulated in userland code. 
 + 
 +If formatting stringy numerical literals is desired, then support for these can be added in the next major version of PHP.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 134: 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 with a 2/3 majority required.+A simple yes/no voting option on whether to support digit separator in PHP. A 2/3 majority is required
 + 
 +<doodle title="Include a Digit Separator into PHP" auth="tpunt" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle> 
 + 
 +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://github.com/php/php-src/pull/1699
  
 ===== Implementation ===== ===== Implementation =====
Line 147: Line 173:
  
 ===== References ===== ===== References =====
-Current discussion: to be updated...+Current discussion: https://marc.info/?l=php-internals&m=145149644624888&w=2
  
 Previous discussion on separators for numerical literals: https://marc.info/?l=php-internals&m=142431171323037&w=2 Previous discussion on separators for numerical literals: https://marc.info/?l=php-internals&m=142431171323037&w=2
rfc/number_format_separator.1450907644.txt.gz · Last modified: 2017/09/22 13:28 (external edit)