golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.86k stars 17.52k forks source link

Numeric literals with spaces as delimeters #42

Closed gopherbot closed 9 years ago

gopherbot commented 14 years ago

by Serge.Sitnikov:

Please could you change the grammar to accept following numeric literals:

const
(
  nanoseconds_per_second = 1 000 000 000;
  packed_primes = 0x 02 03 05 07 0B 0D 11 13;
  epsilon = 0.000 001;
)
agl commented 14 years ago

Comment 1:

Your request isn't unreasonable, but we are very conservative with language features.

Labels changed: added language-change.

rsc commented 14 years ago

Comment 2:

Status changed to Thinking.

gopherbot commented 14 years ago

Comment 3 by michael.jones:

You could implement Serge's good idea using the existing special symbol "_" 
(Underscore) which Go uses to say "present but missing" in other contexts. This 
would make parsing effortless.
const
(
  nanoseconds_per_second = 1_000_000_000;
  packed_primes = 0x_02_03_05_07_0B_0D_11_13;
  epsilon = 0.000_001;
)
It shares most of the benefit, avoids the issue of a spaced-out hexadecimal literal 
with a segment beginning with [a-fA-f] seeming like a non-number, and adds a bit 
of Ada to Go.
gopherbot commented 14 years ago

Comment 4 by joseph.stewart:

Any consideration for Erlang style numeric constants?
They're in the format
BASE_IN_DECIMAL # NUMBER_IN_BASE_WITH_POSSIBLE_UNDERSCORES
examples:
2#111 = 7 dec
16#ff = 255 decimal
36#zzz = 46655 decimal
gopherbot commented 14 years ago

Comment 5 by Serge.Sitnikov:

Agree with Michael. Underscores not so intuitive and clean as spaces, but they will 
leave a room for future language extensions, for something like composite space-
separated set literals.
gopherbot commented 14 years ago

Comment 6 by netchv:

Really Erlang copies Ada approach with small change (Ada #p#n -> Erlang p#n). I also
vote for this - it gives much readability without any syntax conflict.
Also I vote for discarding treating "0..." as octal (in #145).
(To justify myself: this of course seems like bikeshed, but IMHO it really influes
the readability)
gopherbot commented 14 years ago

Comment 7 by notan.kdev:

Underlines are used in other languages.  
1_000_000_000 is much more readable than 1000000000
ADA allows it.
the IEC61131-3 Languages for PLC programming to.
jimmyfrasche commented 14 years ago

Comment 8:

I had never considered spaces in numeric literals before but I like it because it
follows the current convention of lexical concatenation of string literals, such as
"A" " string"
gopherbot commented 14 years ago

Comment 9 by JQBalter:

"Your request isn't unreasonable, but we are very conservative with language features."
What a ridiculous comment. How about banning multiple spaces -- or even all
unnecessary spaces, in programs because allowing them isn't "conservative"? What
about unicode identifiers -- *that* isn't conservative!
Allowing underscores within numeric constants is a small upwards-compatible change
localized to the lexer -- it barely deserves to be called a "feature".
robpike commented 14 years ago

Comment 10:

Status changed to WontFix.