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/internals/syntax.md
+12-9Lines changed: 12 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -96,14 +96,17 @@ The lexical analyzer also inserts `indent` and `outdent` tokens that represent r
96
96
In the context-free productions below we use the notation `<<< ts >>>`
97
97
to indicate a token sequence `ts` that is either enclosed in a pair of braces `{ ts }` or that constitutes an indented region `indent ts outdent`. Analogously, the
98
98
notation `:<<< ts >>>` indicates a token sequence `ts` that is either enclosed in a pair of braces `{ ts }` or that constitutes an indented region `indent ts outdent` that follows
99
-
a `:` at the end of a line.
99
+
a `colon` token.
100
100
101
+
A `colon` token reads as the standard colon "`:`" but is generated instead of it where `colon` is legal according to the context free syntax, but only if the previous token
102
+
is an alphanumeric identifier, a backticked identifier, or one of the tokens `this`, `super`, "`)`", and "`]`".
101
103
102
104
```
105
+
colon ::= ':' -- with side conditions explained above
By and large, the possible indentation regions coincide with those regions where braces `{...}` are also legal, no matter whether the braces enclose an expression or a set of definitions. There is one exception, though: Arguments to function can be enclosed in braces but they cannot be simply indented instead. Making indentation always significant for function arguments would be too restrictive and fragile.
8
-
9
-
To allow such arguments to be written without braces, a variant of the indentation scheme is implemented under language import
10
-
```scala
11
-
importlanguage.experimental.fewerBraces
12
-
```
13
-
Alternatively, it can be enabled with command line option `-language:experimental.fewerBraces`.
14
-
15
-
This variant is more contentious and less stable than the rest of the significant indentation scheme. It allows to replace a function argument in braces by a `:` at the end of a line and indented code, similar to the convention for class bodies. The `:` can
16
-
optionally be followed by the parameter part of a function literal.
17
-
18
-
## Using `:` At End Of Line
19
-
20
-
21
-
Similar to what is done for classes and objects, a `:` that follows a function reference at the end of a line means braces can be omitted for function arguments. Example:
22
-
```scala
23
-
times(10):
24
-
println("ah")
25
-
println("ha")
26
-
```
27
-
28
-
The colon can also follow an infix operator:
29
-
30
-
```scala
31
-
credentials ++:
32
-
valfile=Path.userHome /".credentials"
33
-
if file.exists
34
-
thenSeq(Credentials(file))
35
-
elseSeq()
36
-
```
37
-
38
-
Function calls that take multiple argument lists can also be handled this way:
39
-
40
-
```scala
41
-
valfirstLine= files.get(fileName).fold:
42
-
valfileNames= files.values
43
-
s"""no file named $fileName found among
44
-
|${values.mkString(\n)}""".stripMargin
45
-
:
46
-
f =>
47
-
vallines= f.iterator.map(_.readLine)
48
-
lines.mkString("\n)
49
-
```
50
-
51
-
52
-
## Lambda Arguments Without Braces
53
-
54
-
The `:` can optionally be followed by the parameter part of a function literal:
55
-
```scala
56
-
val xs = elems.map: x =>
57
-
val y = x - 1
58
-
y * y
59
-
xs.foldLeft(0): (x, y) =>
60
-
x + y
61
-
```
62
-
Braces can be omitted if the lambda starts with a parameter list and an arrow symbol `=>` or `?=>`.
63
-
The arrow is followed on the next line(s) by the body of the functional literal which must be indented
64
-
relative to the previous line.
65
-
66
-
## Syntax Changes
67
-
68
-
As a lexical change, a `:` at the end of a line is now always treated as a
Copy file name to clipboardExpand all lines: docs/_docs/reference/other-new-features/indentation.md
+53-29Lines changed: 53 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,7 @@ There are two rules:
61
61
62
62
- after the leading parameters of an `extension`, or
63
63
- after a `with` in a giveninstance, or
64
-
- after a ": at end of line" token (see below)
64
+
- after a `:` at the start of a template body (see discussion of `<colon>` below), or
65
65
- after one of the following tokens:
66
66
67
67
```
@@ -134,12 +134,14 @@ is parsed as `if x then a + b + c else d`.
134
134
135
135
TheScala grammar uses the term _template body_ for the definitions of a class, trait, or objectthat are normally enclosed in braces. The braces around a template body can also be omitted by means of the following rule.
136
136
137
-
If at the point where a template body can start there is a `:` that occurs at the end
138
-
of a line, and that is followed by at least one indented statement, the recognized
139
-
token is changed from ":"to ": at end of line". The latter token is one of the tokens
140
-
that can start an indentation region. TheScala grammar is changed so an optional ": at end of line" is allowed in front of a template body.
137
+
A template body can alternatively consist of a colon followed by one or more indented statements. Tothis purpose we introduce a new `<colon>` token that reads as
138
+
the standard colon "`:`" but is generated instead of it where `<colon>`
139
+
is legal according to the context free syntax, but only ifthe previous token
140
+
is an alphanumeric identifier, a backticked identifier, or one of the tokens `this`, `super`, "`)`", and "`]`".
141
141
142
-
Analogous rules apply forenumbodies and local packages containing nested definitions.
142
+
An indentation region can start after a `<colon>`. A template body may be either enclosed in braces, or it may start with
143
+
`<colon> <indent>` and end with`<outdent>`.
144
+
Analogous rules apply forenumbodies, typerefinements, and local packages containing nested definitions.
143
145
144
146
With these new rules, the following constructs are all valid:
145
147
@@ -170,17 +172,19 @@ In each case, the `:` at the end of line can be replaced without change of meani
170
172
171
173
The syntax changes allowing this are asfollows:
172
174
175
+
Definefor an arbitrary sequence of tokens or non-terminals `TS`:
Here, `colonEol` stands for": at end of line", asdescribed above.
181
-
The lexical analyzer is modified so that a `:` at the end of a line
182
-
is reported as `colonEol` if the parser is at a point where a `colonEol` is
183
-
valid asnext token.
184
188
185
189
### Spaces vs Tabs
186
190
@@ -444,15 +448,15 @@ indented regions where possible. When invoked with options `-rewrite -no-indent`
444
448
The `-indent` option only works on [new-style syntax](./control-syntax.md). So to go from old-style syntax to new-style indented code one has to invoke the compiler twice, first with options `-rewrite -new-syntax`, then again with options
445
449
`-rewrite -indent`. To go in the opposite direction, from indented code to old-style syntax, it's `-rewrite -no-indent`, followed by `-rewrite -old-syntax`.
446
450
447
-
###Variant:IndentationMarker `:`
451
+
###Variant:IndentationMarker `:`forArguments
448
452
449
-
Generally, the possible indentation regions coincide with those regions where braces `{...}` are also legal, no matter whether the braces enclose an expression or a set of definitions. There is one exception, though: Arguments to function can be enclosed in braces but they cannot be simply indented instead. Making indentation always significant for function arguments would be too restrictive and fragile.
453
+
Generally, the possible indentation regions coincide with those regions where braces `{...}` are also legal, no matter whether the braces enclose an expression or a set of definitions. There is one exception, though: Arguments to functions can be enclosed in braces but they cannot be simply indented instead. Making indentation always significant for function arguments would be too restrictive and fragile.
450
454
451
455
To allow such arguments to be written without braces, a variant of the indentation scheme is implemented under language import
452
456
```scala
453
457
importlanguage.experimental.fewerBraces
454
458
```
455
-
This variant is more contentious and less stable than the rest of the significant indentation scheme. Inthis variant, a colon `:` at the end of a line is also one of the possible tokens that opens an indentation region. Examples:
459
+
Inthis variant, a `<colon>` token is also recognized where function argument would be expected. Examples:
456
460
457
461
```scala
458
462
times(10):
@@ -462,24 +466,44 @@ times(10):
462
466
463
467
or
464
468
469
+
```scala
470
+
credentials `++`:
471
+
valfile=Path.userHome /".credentials"
472
+
if file.exists
473
+
thenSeq(Credentials(file))
474
+
elseSeq()
475
+
```
476
+
477
+
or
478
+
465
479
```scala
466
480
xs.map:
467
481
x =>
468
482
valy= x -1
469
483
y * y
470
484
```
471
-
472
-
The colon is usable not only for lambdas and by-name parameters, but
473
-
also even for ordinary parameters:
485
+
What's more, a `:` in these settings can also be followed on the same line by the parameter part and arrow of a lambda. So the last example could be compressed to this:
474
486
475
487
```scala
476
-
credentials ++:
477
-
valfile=Path.userHome /".credentials"
478
-
if file.exists
479
-
thenSeq(Credentials(file))
480
-
elseSeq()
488
+
xs.map: x =>
489
+
valy= x -1
490
+
y * y
491
+
```
492
+
and the following would also be legal:
493
+
```scala
494
+
xs.foldLeft: (x, y) =>
495
+
x + y
481
496
```
482
497
483
-
How does this syntax variant work?Colons at the end of lines are their own token, distinct from normal `:`.
484
-
TheScala grammar is changed so that colons at end of lines are accepted at all points
485
-
where an opening brace enclosing an argument is legal. Special provisions are taken so that method result types can still use a colon on the end of a line, followed by the actual typeon the next.
498
+
The grammar changes forthis variant are asfollows.
0 commit comments