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
Enables fewerBraces as a standard feature from 3.3 on. No language
import is needed.
The old
import language.experimental.fewerBraces
is deprecated but it still works. This is so that one can continue to
mix fewerBraces in for projects compiling with earlier versions. We can
turn the language import into a no-op or drop it completely when 3.3 is
in the rear mirror.
Starting with Scala 3.3, a `<colon>` token is also recognized where a function argument would be expected. Examples:
192
+
193
+
```scala
194
+
times(10):
195
+
println("ah")
196
+
println("ha")
197
+
```
198
+
199
+
or
200
+
201
+
```scala
202
+
credentials `++`:
203
+
val file = Path.userHome / ".credentials"
204
+
if file.exists
205
+
then Seq(Credentials(file))
206
+
else Seq()
207
+
```
208
+
209
+
or
210
+
211
+
```scala
212
+
xs.map:
213
+
x =>
214
+
val y = x - 1
215
+
y * y
216
+
```
217
+
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:
218
+
219
+
```scala
220
+
xs.map: x =>
221
+
val y = x - 1
222
+
y * y
223
+
```
224
+
and the following would also be legal:
225
+
```scala
226
+
xs.foldLeft(0): (x, y) =>
227
+
x + y
228
+
```
229
+
230
+
The grammar changes for optional braces around arguments are as follows.
231
+
232
+
```
233
+
SimpleExpr ::= ...
234
+
| SimpleExpr ColonArgument
235
+
InfixExpr ::= ...
236
+
| InfixExpr id ColonArgument
237
+
ColonArgument ::= colon [LambdaStart]
238
+
indent (CaseClauses | Block) outdent
239
+
LambdaStart ::= FunParams (‘=>’ | ‘?=>’)
240
+
| HkTypeParamClause ‘=>’
241
+
```
242
+
189
243
## Spaces vs Tabs
190
244
191
245
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.
@@ -448,62 +502,3 @@ indented regions where possible. When invoked with options `-rewrite -no-indent`
448
502
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
449
503
`-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`.
450
504
451
-
##Variant:IndentationMarker `:` forArguments
452
-
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.
454
-
455
-
To allow such arguments to be written without braces, a variant of the indentation scheme is implemented under language import
456
-
```scala
457
-
importlanguage.experimental.fewerBraces
458
-
```
459
-
Inthis variant, a `<colon>` token is also recognized where function argument would be expected. Examples:
460
-
461
-
```scala
462
-
times(10):
463
-
println("ah")
464
-
println("ha")
465
-
```
466
-
467
-
or
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
-
479
-
```scala
480
-
xs.map:
481
-
x =>
482
-
valy= x -1
483
-
y * y
484
-
```
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:
486
-
487
-
```scala
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(0): (x, y) =>
495
-
x + y
496
-
```
497
-
498
-
The grammar changes forthis variant are asfollows.
0 commit comments