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-new.md
+88-39Lines changed: 88 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ layout: doc-page
3
3
title: Optional Braces
4
4
---
5
5
6
-
As an experimental feature, Scala 3 treats indentation as significant and allows
6
+
As an experimental feature, Scala 3 enforces some rules on indentation and allows
7
7
some occurrences of braces `{...}` to be optional.
8
8
9
9
- First, some badly indented programs are ruled out, which means they are flagged with warnings.
@@ -12,7 +12,7 @@ some occurrences of braces `{...}` to be optional.
12
12
13
13
### Indentation Rules
14
14
15
-
The compiler enforces three rules for well-indented programs, flagging violations as warnings.
15
+
The compiler enforces two rules for well-indented programs, flagging violations as warnings.
16
16
17
17
1. In a brace-delimited region, no statement is allowed to start to the left
18
18
of the first statement after the opening brace that starts a new line.
@@ -35,14 +35,6 @@ The compiler enforces three rules for well-indented programs, flagging violation
35
35
println(2) // error: missing `{`
36
36
```
37
37
38
-
3. If significant indentation is turned off, code that follows a classorobjectdefinition (or similar) lacking a `{...}` body may not be indented more than that definition. This prevents misleading situations like:
39
-
40
-
```scala
41
-
traitA
42
-
caseclassB() extendsA// error: indented too far to the right
43
-
```
44
-
It requires that the caseclass`C` to be written instead at the same level of indentation asthetrait`A`.
45
-
46
38
These rules still leave a lot of leeway how programs should be indented. For instance, they do not impose
47
39
any restrictions on indentation within expressions, nor do they require that all statements of an indentation block line up exactly.
48
40
@@ -66,11 +58,10 @@ There are two rules:
66
58
An indentation region can start
67
59
68
60
- after the condition of an `if-else`, or
69
-
- at points where a set of definitions enclosed in braces is expected in a
70
-
class, object, given, or enumdefinition, in an enumcase, or after a packageclause, or
61
+
- after the leading parameter(s) of a givenextension method clause, or
If an `<indent>` is inserted, the indentation width of the token on the next line
76
67
is pushed onto `IW`, which makes it the new current indentation width.
@@ -97,6 +88,66 @@ if x < 0
97
88
Indentation tokens are only inserted in regions where newline statement separators are also inferred:
98
89
at the toplevel, inside braces `{...}`, but not inside parentheses `(...)`, patterns or types.
99
90
91
+
###NewRole of With
92
+
93
+
To make bracews optional for constructs like classbodies, the syntax of the language is changed so that a classbody or similar construct may optionally be prefixed with`with`. Since `with` can start an indentation region, this means that all of the following syntaxes are allowed and are equivalent:
It is assumed here that braces following a `with` can be transparently replaced by an
137
+
indentation region.
138
+
139
+
With the new indentation rules, the previously allowed syntax
140
+
```
141
+
classAextendsBwith
142
+
C
143
+
```
144
+
becomes illegal since `C` above would be terated asa nested statement inside `A`. More generally, a `with` that separates parent constructors cannot be at the end of a line. One has to write
145
+
```
146
+
classAextendsB
147
+
withC
148
+
```
149
+
instead (or replace the "`with`" by a "`,`"). When compiling in Scala-2 mode, a migration warning is issued for the illegal syntax and a (manual) rewrite is suggested.
150
+
100
151
###Spaces vs Tabs
101
152
102
153
Indentation prefixes can consist of spaces and/or tabs. Indentation widths are the indentation prefixes themselves, ordered by the string prefix relation. So, so for instance "2 tabs, followed by 4 spaces" is strictly less than "2 tabs, followed by 5 spaces", but "2 tabs, followed by 4 spaces" is incomparable to "6 tabs" or to "4 spaces, followed by 2 tabs". It is an error if the indentation width of some line is incomparable with the indentation width of the region that's current at that point. To avoid such errors, it is a good idea not to mix spaces and tabs in the same source file.
@@ -165,46 +216,44 @@ It is recommended that `end` markers are used for code where the extent of an in
165
216
Here is a (somewhat meta-circular) example of code using indentation. It provides a concrete representation of indentation widths asdefined above together with efficient operations for constructing and comparing indentation widths.
0 commit comments