← Back to DAGDA | Support | Source: https://github.com/dotnet/roslyn/issues/216
Skip to content

Proposal: Digit separators #216

Closed
Closed

Description

@MadsTorgersen

Being able to group digits in large numeric literals would have great readability impact and no significant downside.

Adding binary literals (#215) would increase the likelihood of numeric literals being long, so the two features enhance each other.

We would follow Java and others, and use an underscore _ as a digit separator. It would be able to occur everywhere in a numeric literal (except as the first and last character), since different groupings may make sense in different scenarios and especially for different numeric bases:

int bin = 0b1001_1010_0001_0100;
int hex = 0x1b_a0_44_fe;
int dec = 33_554_432;
int weird = 1_2__3___4____5_____6______7_______8________9;
double real = 1_000.111_1e-1_000;

Any sequence of digits may be separated by underscores, possibly more than one underscore between two consecutive digits. They are allowed in decimals as well as exponents, but following the previous rule, they may not appear next to the decimal (10_.0), next to the exponent character (1.1e_1), or next to the type specifier (10_f). When used in binary and hexadecimal literals, they may not appear immediately following the 0x or 0b.

The syntax is straightforward, and the separators have no semantic impact - they are simply ignored.

This has broad value and is easy to implement.

Activity

  1. self-assigned this
    on Feb 3, 2015
  2. svick commented on Feb 3, 2015

    @svick
    Member

    Does this apply to real literals as well? For example, would 1_0_._5_e_-_1_6_m_ be valid?

    I have no idea if this would be useful, just curious.

  3. monoman commented on Feb 4, 2015

    @monoman

    👍

  4. chrisaut commented on Feb 4, 2015

    @chrisaut

    Don't shoot me, but would it be too hard to parse "space" as a seperator? Or does that make the grammer ambiguous?

    int two = 0b 10;
    short max = 0x ffff;
    long oneMillion = 1 000 000;

    Just thinking out loud.

  5. AdamSpeight2008 commented on Feb 4, 2015

    @AdamSpeight2008
    Contributor

    Digit separators where included in the VB.net (vNext CTP) would it be beneficial to a also describe what was allow in VB? It think
    '1_000' was allowed but 1__000 wasn't.
    Comma usage could be an issue as it would clash with array literals, as you couldn't tell what was number and what was and array element.

  6. mburbea commented on Feb 4, 2015

    @mburbea

    I agree I like space more then underscore. It's generally easier to type and makes it easier when working with something like hex numbers e.g.
    0x8080 8080 8080 8080UL is so much easier to read and make sure I've filled all the slots vs something like 0x8080808080808080UL where I have to sit and count to see if I got 16 characters or I only typed 14 or something. How's about ' as well.

  7. alanfo commented on Feb 6, 2015

    @alanfo

    I don't see how you could use a space as the separator because numeric literals would then potentially consist of not one but several tokens. This would make them very difficult to parse.

    The underscore seems the best choice of separator to me, particularly as it's already used by several other languages.

    I'm not so keen on allowing multiple consecutive underscores but I suppose it does no harm.

  8. AdamSpeight2008 commented on Feb 6, 2015

    @AdamSpeight2008
    Contributor

    This grammar wouldn't allow consecutive separators.

      digit ::= '0' - '9'
      sep   ::= '_'
     prefix ::=
    literal ::= prefix (sep? digit)+
    

    I think spaces could also be possible

        digit ::= '0'-'9'
    seperator ::= ' '
      literal ::= digit (separator? digit)*
    
  9. paulomorgado commented on Feb 6, 2015

    @paulomorgado

    I think it would be very hard to use spaces.

    I haven't looked at the parser, but it's probably doing something like breaking the text at white spaces, parenthesis, braces, whatever and analyzing the tokens from there. Assuming that after a numeric literal it might come the rest of it is doable, but I don't think it is worth the cost.

    And what next? This?

    var a = 1111
            1111
            1111
            1111;
    

    Or this?

    var a = 1111    // comment
            1111    // comment
            1111    // comment
            1111;   // comment
    

    Although it might be an itsy bitsy harder to write in most keyboard configuration, the semantic break of the numeric literal is the same with the _ and I would argue that it's even better because gives separation and cohesion.

  10. AdamSpeight2008 commented on Feb 6, 2015

    @AdamSpeight2008
    Contributor

    Wonder if the parser supports significant whitespace?

  11. AnthonyDGreen commented on Feb 7, 2015

    @AnthonyDGreen
    Contributor

    The VB implementation of digit group separators prototyped last year actually supported three different separators originally: underscore, back tick, and space. So you could write &B1111 0010 or 1_000_000 or 3`600. We quickly decided that back tick didn't make enough sense to anyone and cut it. The VB preview still supported both underscores and spaces. The biggest motivation for spaces was binary literals, another feature prototyped at the same time, because binary numbers are conventionally separated with spaces.

    As to implementation, it's not hard at all really - at least in VB, particularly when you don't allow multiple consecutive separators. Normally the scanner encounters a digit and starts scanning a integral literal one character at a time until it encounters a character that's not a digit for the base being used (decimal, hex, octal) then it stops. We changed it so that if the non-digit character were a underscore or space it would peek one more character ahead and if that character were a digit it would keep scanning it as a single token. There are some corner cases you have to put extra recovery around but it's not very complicated, particularly because in VB it's not valid to have two integer literals follow one another so it's non-breaking to interpret 1 1 as 11. I think C# is the same here though in C# we were pretty settled that underscore would be the sole separator.

    I think the biggest concern about that is that tools would be confused thinking the space was a word boundary (not VS, the editor is smart enough in VS to handle space) and we just couldn't foresee what havoc spaces would be unleashing on the world (if any).

    Another more minor concern was complexity - would users benefit more from having a single consistent separator used everywhere? If we decided to pick one it would likely be the underscore so space was only a possibility if we were ok with having two separators which was an open question.

    -ADG

  12. 37 remaining items

  13. weitzhandler commented on Mar 6, 2017

    @weitzhandler
    Contributor

    Would be nice. Although space feels less C#ish, I still vote for spaces, I mean can it go wrong as long as we're expecting a ;?
    Anyway, I think it should only be allowed in binary/hex/o̷c̷t̷ etc.?

  14. paulomorgado commented on Mar 6, 2017

    @paulomorgado

    @weitzhandler, I think that changing C# 7 and Visual Studio for tomorrow is, most probably, out of the question. 😄

  15. alrz commented on Mar 6, 2017

    @alrz
    Member

    so

    var a = 1                                                                                                                                                                                                                                      0;

    is actually just ten?

  16. paulomorgado commented on Mar 6, 2017

    @paulomorgado

    @alrz, that's no worst than

    var a = 1______________________________________________________________________________________________________________________________________________________________________________________________________________________________________0;
    

    The greater issue here is that, in this particular case and only in this particular case, space is a special case for white spaces. And that's bad. Very bad.

  17. alrz commented on Mar 6, 2017

    @alrz
    Member

    @paulomorgado

    No, the space is worst because it's invisible. In your example it's impossible to overlook the zero because the literal goes on and on. and on.

  18. weitzhandler commented on Mar 6, 2017

    @weitzhandler
    Contributor

    Limit to single space (surely no line breaks 😡).

    _ is definitely more C#ish anyway.
    And separation only make sense in binary/hex.

  19. alrz commented on Mar 6, 2017

    @alrz
    Member

    @weitzhandler No it doesn't. C# doesn't mind how many spaces you are using between tokens at all.

  20. weitzhandler commented on Mar 6, 2017

    @weitzhandler
    Contributor

    We should keep the discussion here.

  21. gafter commented on Mar 6, 2017

    @gafter
    Member

    @weitzhandler I think you mean discussion has moved here.

  22. paulomorgado commented on Mar 7, 2017

    @paulomorgado

    @alrz, Visual Studio can make white space visible. But I still think that would be the least of the problems.

  23. gafter commented on Mar 7, 2017

    @gafter
    Member

    Discussion for this feature has been moved here.

  24. added
    Resolution-External
    and removed
    4 - In Review
    on Mar 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area-Language DesignFeature RequestFeature SpecificationLanguage-C#Resolution-External

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions