Skip to content

Commit

Permalink
add digit separators to language tour
Browse files Browse the repository at this point in the history
  • Loading branch information
MaryaBelanger committed Oct 29, 2024
1 parent aa244d3 commit 65e9940
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 11 additions & 0 deletions examples/misc/lib/language_tour/built_in_types.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: dead_code, unused_local_variable, type_annotate_public_apis
// ignore_for_file: prefer_single_quotes, prefer_collection_literals
// ignore_for_file: experiment_not_enabled

void miscDeclAnalyzedButNotTested() {
{
Expand Down Expand Up @@ -37,6 +38,16 @@ void miscDeclAnalyzedButNotTested() {
// #enddocregion const-num
}

{
// #docregion digit-separators
var n1 = 1_000_000;
var n2 = 0.000_000_000_01;
var n3 = 0x00_14_22_01_23_45; // MAC address
var n4 = 555_123_4567; // US Phone number
var n5 = 100__000_000__000_000; // one hundred million million!
// #enddocregion digit-separators
}

{
// #docregion quoting
var s1 = 'Single quotes work well for string literals.';
Expand Down
16 changes: 14 additions & 2 deletions src/content/language/built-in-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ assert((3 & 4) == 0); // 0011 & 0100 == 0000
For more examples, see the
[bitwise and shift operator][] section.

Literal numbers are compile-time constants.
Number literals are compile-time constants.
Many arithmetic expressions are also compile-time constants,
as long as their operands are
compile-time constants that evaluate to numbers.
Expand All @@ -173,6 +173,18 @@ const msUntilRetry = secondsUntilRetry * msPerSecond;

For more information, see [Numbers in Dart][dart-numbers].

You can use one or more underscores (`_`) as digit separators
to make long number literals more readable.
Multiple digit separators allow for higher level grouping.

<?code-excerpt "misc/lib/language_tour/built_in_types.dart (digit-separators)"?>
```dart
var n1 = 1_000_000;
var n2 = 0.000_000_000_01;
var n3 = 0x00_14_22_01_23_45; // MAC address
var n4 = 555_123_4567; // US Phone number
var n5 = 100__000_000__000_000; // one hundred million million!
```

## Strings

Expand Down Expand Up @@ -254,7 +266,7 @@ var s = r'In a raw string, not even \n gets special treatment.';
See [Runes and grapheme clusters](#runes-and-grapheme-clusters) for details on how
to express Unicode characters in a string.

Literal strings are compile-time constants,
String literals are compile-time constants,
as long as any interpolated expression is a compile-time constant
that evaluates to null or a numeric, string, or boolean value.

Expand Down

0 comments on commit 65e9940

Please sign in to comment.