@@ -71,15 +71,12 @@ routine argument lists.
71
71
alignment\ (1,2,3,4).say;
72
72
long-name-alignment(3,5)\ .say;
73
73
74
- = head2 Separating Statements
74
+ = head2 Separating Statements with Semicolons
75
75
76
76
A Perl 6 program is a list of statements, separated by semicolons C < ; > .
77
77
A semicolon after the final statement (or after the final statement inside a
78
78
block) is optional, though it's good form to include it.
79
79
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
-
83
80
= begin code
84
81
if True {
85
82
say "Hello";
@@ -90,15 +87,24 @@ say "world";
90
87
Both semicolons are optional here, but leaving them out increases the chance
91
88
of syntax errors when adding more lines later.
92
89
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.
94
99
95
100
= begin code
96
101
if True { say "Hello" }; say "world";
97
102
# ^^^ this ; is required
98
103
= end code
99
104
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:
102
108
103
109
= begin code
104
110
if True { say "Hello" } else { say "Goodbye" }; say "world";
@@ -112,10 +118,21 @@ say "world";
112
118
113
119
= begin code
114
120
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
116
123
say "world";
117
124
= end code
118
125
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
+
119
136
= head2 Comments
120
137
121
138
Comments are parts of the program text only intended for human readers, and
0 commit comments