rfc:numeric_literal_separator
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:numeric_literal_separator [2019/05/15 15:04] – Link to implementation and change status to Under Discussion theodorejb | rfc:numeric_literal_separator [2019/08/19 19:58] (current) – Separators in C# aren't just a proposal theodorejb | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Author: Theodore Brown < | * Author: Theodore Brown < | ||
* Based on [[https:// | * Based on [[https:// | ||
- | * Status: | + | * Status: |
+ | * Discussion: https:// | ||
* Target version: PHP 7.4 | * Target version: PHP 7.4 | ||
* Implementation: | * Implementation: | ||
Line 44: | Line 45: | ||
299_792_458; | 299_792_458; | ||
0xCAFE_F00D; | 0xCAFE_F00D; | ||
- | 0b0010_1101; // binary | + | 0b0101_1111; // binary |
- | 026_73_43; | + | 0137_041; // octal |
</ | </ | ||
Line 68: | Line 69: | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
- | Underscores | + | Adding an underscore between digits |
- | lexing stage, so the runtime | + | change its value. The underscores are stripped out during the lexing |
+ | stage, so the runtime | ||
<code php> | <code php> | ||
Line 96: | Line 98: | ||
constants, unit test values, and performing data conversions. | constants, unit test values, and performing data conversions. | ||
For example: | For example: | ||
- | |||
- | Bugsnag' | ||
- | |||
- | <code php> | ||
- | const MAX_SIZE = 500000; // without separator | ||
- | |||
- | const MAX_SIZE = 500_000; // with separator | ||
- | </ | ||
Composer' | Composer' | ||
Line 117: | Line 111: | ||
<code php> | <code php> | ||
- | $time = (int) ($adTime / 10000000 - 11644473600); | + | $time = (int) ($adTime / 10000000 - 11644473600); |
- | $time = (int) ($adTime / 10_000_000 - 11_644_473_600); | + | $time = (int) ($adTime / 10_000_000 - 11_644_473_600); |
+ | </ | ||
+ | |||
+ | Working with scientific constants: | ||
+ | |||
+ | <code php> | ||
+ | const ASTRONOMICAL_UNIT = 149597870700; | ||
+ | |||
+ | const ASTRONOMICAL_UNIT = 149_597_870_700; | ||
+ | </ | ||
+ | |||
+ | Separating bytes in a binary or hex literal: | ||
+ | |||
+ | <code php> | ||
+ | 0b01010100011010000110010101101111; | ||
+ | |||
+ | 0b01010100_01101000_01100101_01101111; | ||
+ | |||
+ | 0x42726F776E; | ||
+ | |||
+ | 0x42_72_6F_77_6E; | ||
</ | </ | ||
Line 126: | Line 140: | ||
It may be tempting to use integers for storing data such as phone, | It may be tempting to use integers for storing data such as phone, | ||
credit card, and social security numbers since these values appear | credit card, and social security numbers since these values appear | ||
- | numeric. However, this is almost always a bad idea, since these | + | numeric. However, this is almost always a bad idea, since such |
- | values | + | numbers |
A good rule of thumb is that if it doesn' | A good rule of thumb is that if it doesn' | ||
mathematical operators on a value (e.g. adding it, multiplying it, | mathematical operators on a value (e.g. adding it, multiplying it, | ||
- | dividing it, etc.), then an integer probably isn't the best way to | + | etc.), then an integer probably isn't the best way to store it. |
- | store it. | + | |
<code php> | <code php> | ||
- | // never do this: | + | // don' |
$phoneNumber = 345_6789; | $phoneNumber = 345_6789; | ||
$creditCard = 231_6547_9081_2543; | $creditCard = 231_6547_9081_2543; | ||
Line 150: | Line 163: | ||
binary, octal, decimal, hexadecimal, | binary, octal, decimal, hexadecimal, | ||
practice, this isn't problematic as long as a codebase is consistent. | practice, this isn't problematic as long as a codebase is consistent. | ||
+ | |||
+ | Furthermore, | ||
+ | To use an earlier example, 13_500 and 135_00 could be differentiated | ||
+ | in a find/ | ||
+ | literal, which allows searching for a value like " | ||
+ | the numbers containing that specific byte. | ||
+ | |||
+ | ==== Should it be the role of an IDE to group digits? ==== | ||
+ | |||
+ | It has been suggested that numeric literal separators aren't needed | ||
+ | for better readability, | ||
+ | display large numbers in groups of three digits. | ||
+ | |||
+ | However, it isn't always desirable to group numbers the same way. | ||
+ | For example, a programmer may write '' | ||
+ | depending on whether or not it represents a financial quantity stored | ||
+ | as cents: | ||
+ | |||
+ | <code php> | ||
+ | $total = 100_500_00; // represents $100,500.00 stored as cents | ||
+ | |||
+ | $total = 10_050_000; // represents $10,050,000 | ||
+ | </ | ||
+ | |||
+ | Binary and hex literals may also be grouped by a varying number of | ||
+ | digits to reflect how they are used (e.g. bits may be separated into | ||
+ | nibbles, bytes, or words). An IDE cannot do this automatically | ||
+ | without knowing the programmer' | ||
==== Why resurrect this proposal? ==== | ==== Why resurrect this proposal? ==== | ||
Line 171: | Line 212: | ||
Andrea Faulds summarized the considerations [[https:// | Andrea Faulds summarized the considerations [[https:// | ||
- | > This feature offers some benefit in some cases. It doesn' | + | < |
- | > much new complexity. There' | + | This feature offers some benefit in some cases. It doesn' |
- | > the form of the existing number tokens. It fits in well [with] what' | + | much new complexity. There' |
- | > already there, consistently applying to all number literals. It follows | + | the form of the existing number tokens. It fits in well [with] what' |
- | > established convention in other languages. Its appearance at least hints | + | already there, consistently applying to all number literals. It follows |
- | > that values with these separators are not constants or identifiers, | + | established convention in other languages. Its appearance at least hints |
- | > numbers, reducing potential for confusion. It limits its own application | + | that values with these separators are not constants or identifiers, |
- | > to prevent abuse (no leading, trailing, or repeated separators). And | + | numbers, reducing potential for confusion. It limits its own application |
- | > it's relatively intuitive. | + | to prevent abuse (no leading, trailing, or repeated separators). And |
+ | it's relatively intuitive. | ||
+ | </ | ||
==== Comparison to other languages ==== | ==== Comparison to other languages ==== | ||
Line 186: | Line 229: | ||
* Ada: single, between digits [[http:// | * Ada: single, between digits [[http:// | ||
- | * C# (proposal for 7.0): multiple, between digits [[https:// | + | * C#: multiple, between digits [[https:// |
* C++: single, between digits (single quote used as separator) [[http:// | * C++: single, between digits (single quote used as separator) [[http:// | ||
* Java: multiple, between digits [[https:// | * Java: multiple, between digits [[https:// | ||
Line 200: | Line 243: | ||
===== Vote ===== | ===== Vote ===== | ||
- | Add numeric literal | + | Voting started 2019-05-30 and ended 2019-06-13. |
+ | |||
+ | <doodle title=" | ||
+ | | ||
+ | | ||
+ | </ | ||
===== References ===== | ===== References ===== | ||
Line 208: | Line 256: | ||
Discussion from previous RFC: https:// | Discussion from previous RFC: https:// | ||
- | Blog post about implementation: | + | Blog post about original |
rfc/numeric_literal_separator.1557932647.txt.gz · Last modified: 2019/05/15 15:04 by theodorejb