Skip to content

proposal: permit blank (_) separator in integer number literals #28493

Closed

Description

@griesemer

This has come up before, specifically during discussions of #19308 and #28256. I am writing this down so we have a place for discussing this independently. I have no strong feelings about this proposal either way.

Proposal

This is a fully backward-compatible proposal for permitting the use of a blank (_) as a separator in number literals. Specifically, we change the integer literal syntax such that we also allow a "_" after the first digit (the change is the extra "_"):

int_lit = decimal_lit | octal_lit | hex_lit .
decimal_lit = ( "1" … "9" ) { decimal_digit | "_" } .
octal_lit = "0" { octal_digit | "_" } .
hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit | "_" } .

And we change the floating-point number literal syntax correspondingly (the change is the extra "_"):

float_lit = decimals "." [ decimals ] [ exponent ] | decimals exponent | "." decimals [ exponent ] .
decimals = decimal_digit { decimal_digit | "_" } .
exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .

For complex number literals the change is implied by the change to floating-point literals.

Examples:

123_456_789
1_000_000_000
0b0110_1101_1101_0010  // if we had binary integer literals
0xdead_beef
3.1415_9265

but also:

0___         // 0
0_77         // 77 decimal, or perhaps not permitted (TBD)
01_10        // 0110 (octal)
1_2__3___    // 123
0x1_23_0     // 0x1230
0__.0__e-0__ // 0.0e-0

Discussion

The notation follows more or less the syntax used in other languages (e.g., Swift).
The implementation is straight-forward (a minor change to the number scanner). As the examples show, the separator, if judiciously used, may improve readability; or it may degrade it significantly.
A tool (go vet) might impose restrictions on how the separator is used for readability.

Activity

  1. added this to the Proposal milestone on Oct 30, 2018
  2. ericlagergren commented on Oct 30, 2018

    @ericlagergren
    Contributor

    What would be the reason for allowing the underscore to appear anywhere instead of limiting it to (multiples of) commonly used places? E.g.,

    1_000_000_000
    0xDEAD_BEEF
    0b0101_1111_0001
    

    ISTM being too permissive only degrades readability.

  3. dgryski commented on Oct 30, 2018

    @dgryski
    Contributor

    @ericlagergren It seems more Go-like to leave the grammar more free-form with regards to placement of _, but leave the enforcement of style (how many digits in each group) to tooling and code review.

  4. ericlagergren commented on Oct 30, 2018

    @ericlagergren
    Contributor
  5. griesemer commented on Oct 30, 2018

    @griesemer
    ContributorAuthor

    @ericlagergren What's a commonly used placement of _? That's really difficult to impossible to answer and shouldn't be decided by the language. Also, note that other languages also don't try to pin that down.

  6. ericlagergren commented on Oct 30, 2018

    @ericlagergren
    Contributor
  7. alanfo commented on Oct 31, 2018

    @alanfo

    I think this is an excellent idea if we are to have binary literals (which otherwise become unreadable after one or two bytes) and also helps with the readability of other long numbers.

    Several other languages have a similar feature.

    As far as placement is concerned, we'll just have to leave that to the good taste of gophers :)

  8. davecheney commented on Oct 31, 2018

    @davecheney
    Contributor

    I don’t think this improves readability. For example, which is more readable?

    x := 100000000
    y := 100_000_000
    const million = 1000000
    z := 100 * million

    The latter is, IMO, the more descriptive, leverages the qualities of untyped constants, the lineage of the time package, and exists today without new syntax.

  9. alanfo commented on Oct 31, 2018

    @alanfo

    Well, exact powers of 10 can always be dealt with in some other manner. One could also do this:

    z2 := int(1e8)

    But what about these:

    // binary literals
    a := 0b0101110110011011
    b := 0b01011101_10011011 // bytes
    c := 0b0101_1101_1001_1011 // nybbles

    // decimal literals
    d := 2861945612983
    e := 2_861_945_612_983 // thousand separators

    To my eyes, the underscored literals are much easier to read.

  10. mundaym commented on Oct 31, 2018

    @mundaym
    Member

    Are there any concrete examples of existing code where this would help readability significantly? Adding a separator does make sense for big numbers but are they common enough to warrant changing the language for?

    I've spent a bit of time working with Swift code and in my limited experience I've seen that underscores tend to get used inconsistently. I think they end up making it harder (for me anyway) to parse the code.

    Go already has quite a few ways to write constants, I'm not sure we need more.

  11. alanfo commented on Oct 31, 2018

    @alanfo

    When programming in other languages which have digit separators (C#, Java, Kotlin), I generally split binary literals into nybbles and add thousand separators for decimal literals >= 1,000. That more or less mimics how I'd write them down on paper (with space and comma as respective separators).

    Of course, that doesn't mean I find longer numbers without separators unreadable - I'm OK up to about twice those lengths - but, when you have something, you tend to use it.

    Very long decimal literals don't crop up very often (except perhaps powers of 10) but when they do it really is good to have the digit separator in the toolbox :)

    If this proposal is accepted, then no doubt there will be people who abuse it or use it inconsistently but I don't really think it is practical to limit placement in some way. Other languages don't seem to bother either.

  12. deanveloper commented on Oct 31, 2018

    @deanveloper
  13. RalphCorderoy commented on Oct 31, 2018

    @RalphCorderoy

    I think it would be better to rule out adjacent or trailing underscores, and to always want at least one digit between the base indicator and the first underscore, e.g. 0_7 is invalid, effectively ruling out a leading underscore on the value's digits.

    int_lit = decimal_lit | octal_lit | hex_lit .
    decimal_lit = ( "1" … "9" ) { decimal_digit | "_" decimal_digit } .
    octal_lit = "0" [ octal_digit { octal_digit | "_" octal_digit } ] .
    hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit | "_" hex_digit } .
    
  14. 95 remaining items

  15. gopherbot commented on May 2, 2019

    @gopherbot
    Contributor

    Change https://golang.org/cl/174897 mentions this issue: cmd/compile: disable Go1.13 language features for -lang=go1.12 and below

  16. rsc commented on May 7, 2019

    @rsc
    Contributor

    As a reminder, we introduced a new process for these Go 2-related language changes in our blog post blog.golang.org/go2-here-we-come. The Go 1.13 development cycle is now over and it’s time for the final decision.

    The feedback on the Go 2 number literal changes has been strongly positive, with very few negative voices. These changes modernize and harmonize Go’s number literal syntax without adding significant complexity to the language: There is now a uniform prefix notation for the three common non-decimal number bases, which matches the notation used in other modern programming languages. The introduction of hexadecimal floating-point literals addresses a pain point for people concerning themselves with numeric code. The suffix “i” may now be used with any (non-imaginary) number literal to create an imaginary constant in a uniform way. And finally, underscores may be used to split longer literals into groups of digits for improved readability.

    Proposal accepted for Go 1.13. Closing because the changes have landed.

    - rsc for proposal review

  17. gopherbot commented on Jun 27, 2019

    @gopherbot
    Contributor

    Change https://golang.org/cl/184080 mentions this issue: text/scanner: remove AllowDigitSeparator flag again

  18. eacp commented on Jun 29, 2019

    @eacp

    This would reduce probability of success when grepping code for a constant (grep -Rnw 100000 .), which may lead to wrong conclusions by those analyzing the codebase in a hurry.

    Well thats a fair point. I have done that in some JS programs

  19. gopherbot commented on Aug 10, 2019

    @gopherbot
    Contributor

    Change https://golang.org/cl/189718 mentions this issue: compiler: support new numeric literal syntax

  20. locked and limited conversation to collaborators on Aug 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeLanguageChangeSuggested changes to the Go languageNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.ProposalProposal-Acceptedv2An incompatible library change

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions