Thread overview
Why don't underscores in numbers enforce proper formatting?
Mar 06, 2013
ixid
Mar 06, 2013
simendsjo
Mar 06, 2013
simendsjo
Mar 06, 2013
Adam D. Ruppe
Mar 07, 2013
Simen Kjærås
Mar 07, 2013
Era Scarecrow
March 06, 2013
The underscores in values such as 1_000_000 aid readability but DMD doesn't see anything wrong with any placement of underscores as long as they follow a number. Is there any reason to allow uses like 1_00_000, which are typos or exceedingly lazy modifications of value, and not enforce digits to form sets of three after the first underscore?
March 06, 2013
On Wednesday, 6 March 2013 at 21:06:43 UTC, ixid wrote:
> The underscores in values such as 1_000_000 aid readability but DMD doesn't see anything wrong with any placement of underscores as long as they follow a number. Is there any reason to allow uses like 1_00_000, which are typos or exceedingly lazy modifications of value, and not enforce digits to form sets of three after the first underscore?

If someone uses different semantics on their numbers (like ISBN), having underscores in different places will help readability as the underscores can show the semantics of the number.
March 06, 2013
On Wednesday, 6 March 2013 at 21:06:43 UTC, ixid wrote:
> and not enforce digits to form sets of three after the first underscore?

binary: 1000_1010
or binary: 11001001_11110000
hex: 10_10_40_ae
or hex: dead_beef


Decimal ones might do it too, because you can use a decimal literal for other things, like std.conv.octal. Which is a specific case where I don't think you'd split other than on three, it is still something I could imagine.
March 06, 2013
On Wednesday, 6 March 2013 at 21:15:07 UTC, simendsjo wrote:
> On Wednesday, 6 March 2013 at 21:06:43 UTC, ixid wrote:
>> The underscores in values such as 1_000_000 aid readability but DMD doesn't see anything wrong with any placement of underscores as long as they follow a number. Is there any reason to allow uses like 1_00_000, which are typos or exceedingly lazy modifications of value, and not enforce digits to form sets of three after the first underscore?
>
> If someone uses different semantics on their numbers (like ISBN), having underscores in different places will help readability as the underscores can show the semantics of the number.

Norwegian SSNs have this structure for instance: ddmmyyxxxcc where ddmmyy is day/month/year of birth, xxx is an auto increment number (that also shows somewhat the year of birth) and cc is control digits created from xxx where the last digit also indicates gender. This can be grouped in different ways: ddmmyy_xxx_cc, ddmmyy_xxx_c_c etc. based on what you find the most readable. Forcing this to be d_dmmy_yxx_xcc will clearly be suboptimal. Of course, hardcoded SSNs in code is a bad example :)
March 07, 2013
On Wed, 06 Mar 2013 22:06:42 +0100, ixid <nuaccount@gmail.com> wrote:

> The underscores in values such as 1_000_000 aid readability but DMD doesn't see anything wrong with any placement of underscores as long as they follow a number. Is there any reason to allow uses like 1_00_000, which are typos or exceedingly lazy modifications of value, and not enforce digits to form sets of three after the first underscore?

In addition to the examples given by Simen and Adam, China and Japan
place digits in groups of 4 (10 0000 0000), while India uses groups of 2
(10 00 00 00).

-- 
Simen
March 07, 2013
On Thursday, 7 March 2013 at 06:31:39 UTC, Simen Kjærås wrote:
> On Wed, 06 Mar 2013 22:06:42 +0100, ixid <nuaccount@gmail.com> wrote:
>
>> The underscores in values such as 1_000_000 aid readability but DMD doesn't see anything wrong with any placement of underscores as long as they follow a number. Is there any reason to allow uses like 1_00_000, which are typos or exceedingly lazy modifications of value, and not enforce digits to form sets of three after the first underscore?
>
> In addition to the examples given by Simen and Adam, China and Japan place digits in groups of 4 (10 0000 0000), while India uses groups of 2 (10 00 00 00).

 Seems enforcing underscores to a particular number would be a bad thing. Unless you could specify the width of them so it would do a side check to confirm it fits a particular format, but it would then clutter the code perhaps?? Hmmm..

 However using underscores it's less likely to make silly mistakes since you mentally can count and tell the groups are in 2's, 3's, 4's or 5's fairly easily.