You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/reference/other-new-features/indentation.md
+60-3Lines changed: 60 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -187,7 +187,7 @@ println(".")
187
187
188
188
Indentation-based syntax has many advantages over other conventions. But one possible problem is that it makes it hard to discern when a large indentation region ends, since there is no specific token that delineates the end. Braces are not much better since a brace by itself also contains no information about what region is closed.
189
189
190
-
To solve this problem, Scala3 offers an optional `end` marker. Example
190
+
To solve this problem, Scala3 offers an optional `end` marker. Example:
191
191
```scala
192
192
deflargeMethod(...) =
193
193
...
@@ -214,9 +214,66 @@ End markers are allowed in statement sequences. The specifier token `s` of an en
214
214
-If the statement is a packageclausethatreferstopackage`p`, then`s`mustbethesameidentifier`p`.
215
215
-If the statement is an `if`, `while`, `for`, `try`, or `match` statement, then `s` must be that same token.
216
216
217
-
It is recommended that `end` markers are used for code where the extent of an indentation region is not immediately apparent "at a glance". Typicallythis is the caseif an indentation region spans 20 lines or more.
217
+
For instance, the following end markers are all legal:
218
+
```scala
219
+
packagep1.p2:
220
+
221
+
abstractclassC():
222
+
223
+
defthis(x: Int) =
224
+
this()
225
+
if x >0then
226
+
vala:: b =
227
+
x ::Nil
228
+
end val
229
+
vary=
230
+
x
231
+
endy
232
+
while y >0do
233
+
println(y)
234
+
y -=1
235
+
end while
236
+
try
237
+
x match
238
+
case0=> println("0")
239
+
case _ =>
240
+
end match
241
+
finally
242
+
println("done")
243
+
end try
244
+
end if
245
+
endthis
246
+
247
+
deff:String
248
+
endC
249
+
250
+
objectC:
251
+
givenC=
252
+
newC:
253
+
deff="!"
254
+
endf
255
+
endnew
256
+
endgiven
257
+
endC
258
+
259
+
extension on (x: C):
260
+
defff:String= x.f ++ x.f
261
+
endextension
262
+
263
+
endp2
264
+
```
265
+
266
+
####When to UseEndMarkers
267
+
268
+
It is recommended that `end` markers are used for code where the extent of an indentation region is not immediately apparent "at a glance". People will have different preferences what this means, but one can nevertheless give some guidelines that stem from experience. An end marker makes sense if
269
+
270
+
- the construct contains blank lines, or
271
+
- the construct is long, say 15-20 lines or more,
272
+
- the construct ends heavily indented, say 4 indentation levels or more.
273
+
274
+
If none of these criteria apply, it's often better to not use an end marker since the code will be just asclear and more concise. If there are several ending regions that satisfy one of the criteria above, we usually need an end marker only for the outermost closed reason. So cascades of end markers asin the example above are usually better avoided.
218
275
219
-
**Syntax**
276
+
####Syntax
220
277
221
278
```
222
279
EndMarker::= ‘end’ EndMarkerTag-- when followed by EOL
0 commit comments