Skip to content

Commit a2fb0bf

Browse files
authored
Implied separator rule: focus on ending blocks
I've modified the explanation to focus more on the ending blocks than on the ending semicolon. This is to make improvements toward resolving (or at least mitigating) concerns with issue #1471. Also included an example of implied separator rule with a colon method call.
1 parent 46516eb commit a2fb0bf

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

doc/Language/syntax.pod6

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,12 @@ routine argument lists.
7171
alignment\ (1,2,3,4).say;
7272
long-name-alignment(3,5)\ .say;
7373
74-
=head2 Separating Statements
74+
=head2 Separating Statements with Semicolons
7575
7676
A Perl 6 program is a list of statements, separated by semicolons C<;>.
7777
A semicolon after the final statement (or after the final statement inside a
7878
block) is optional, though it's good form to include it.
7979
80-
In general, a closing curly brace followed by a newline character implies a statement
81-
separator, which is why you don't need to write a semicolon after an C<if> statement block.
82-
8380
=begin code
8481
if True {
8582
say "Hello";
@@ -90,15 +87,24 @@ say "world";
9087
Both semicolons are optional here, but leaving them out increases the chance
9188
of syntax errors when adding more lines later.
9289
93-
You do need to include a semicolon between the C<if> block and the say statement if you want them all on one line.
90+
=head2 Implied Separator Rule (for statements ending in blocks)
91+
92+
Complete statements ending in blocks can omit the trailing semicolon, if no
93+
additional statements on the same line follow the the block's closing curly
94+
brace C<}>. This is called the "implied separator rule". For example, you
95+
don't need to write a semicolon after an C<if> statement block as seen above.
96+
97+
If you want additional statements to trail the block, you do need to include
98+
a semicolon between the block and the statement.
9499
95100
=begin code
96101
if True { say "Hello" }; say "world";
97102
# ^^^ this ; is required
98103
=end code
99104
100-
This doesn't happen in series of blocks that are part of the same C<if>/C<elsif>/C<else> (or similar) construct
101-
because they are part of a single statement. The implied separator rule only applies at the end. These three are equivalent:
105+
However, for a series of blocks that are part of the same C<if>/C<elsif>/C<else> (or similar)
106+
construct, the implied separator rule only applies at the end of the last block of that series.
107+
These three are equivalent:
102108
103109
=begin code
104110
if True { say "Hello" } else { say "Goodbye" }; say "world";
@@ -112,10 +118,21 @@ say "world";
112118
113119
=begin code
114120
if True { say "Hello" } # still in the middle of an if/else statement
115-
else { say "Goodbye" } # <- implied statement separator, just after }
121+
else { say "Goodbye" } # <- no semicolon required because it ends in a block
122+
# without trailing statements in the same line
116123
say "world";
117124
=end code
118125
126+
But, remember, this implied statement separator rule applies in other ways that
127+
statements could end with block. For example, in combination with the colon C<:>
128+
syntax for method calls:
129+
130+
=begin code
131+
my @names = <Foo Bar Baz>;
132+
my @upper-case-names = @names.map: { .uc }
133+
# [FOO BAR BAZ]
134+
=end code
135+
119136
=head2 Comments
120137
121138
Comments are parts of the program text only intended for human readers, and

0 commit comments

Comments
 (0)