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: content/fewer-braces.md
+70-1Lines changed: 70 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -31,8 +31,77 @@ It seems very natural to generalize the current class syntax indentation syntax
31
31
32
32
## Proposed solution
33
33
34
-
The proposed solution is described in detail in https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html#variant-indentation-marker--for-arguments.
34
+
The proposed solution is described in detail in https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html#variant-indentation-marker--for-arguments. I inline the relevant sections here:
35
35
36
+
First, here is the spec for colons at ends of lines for template bodies. This is part of official Scala 3. I cited it here for context.
37
+
38
+
> A template body can alternatively consist of a colon followed by one or more indented statements. To this purpose we introduce a new `<colon>` token that reads as
39
+
the standard colon "`:`" but is generated instead of it where `<colon>`
40
+
is legal according to the context free syntax, but only if the previous token
41
+
is an alphanumeric identifier, a backticked identifier, or one of the tokens `this`, `super`, "`)`", and "`]`".
42
+
43
+
> An indentation region can start after a `<colon>`. A template body may be either enclosed in braces, or it may start with
44
+
`<colon> <indent>` and end with `<outdent>`.
45
+
Analogous rules apply for enum bodies, type refinements, and local packages containing nested definitions.
46
+
47
+
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 so far 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.
48
+
49
+
To allow such arguments to be written without braces, a variant of the indentation scheme is implemented under language import
50
+
```scala
51
+
importlanguage.experimental.fewerBraces
52
+
```
53
+
This SIP proposes to make this variant the default, so no language import is needed to enable it.
54
+
In this variant, a `<colon>` token is also recognized where function argument would be expected. Examples:
55
+
56
+
```scala
57
+
times(10):
58
+
println("ah")
59
+
println("ha")
60
+
```
61
+
62
+
or
63
+
64
+
```scala
65
+
credentials `++`:
66
+
valfile=Path.userHome /".credentials"
67
+
if file.exists
68
+
thenSeq(Credentials(file))
69
+
elseSeq()
70
+
```
71
+
72
+
or
73
+
74
+
```scala
75
+
xs.map:
76
+
x =>
77
+
valy= x -1
78
+
y * y
79
+
```
80
+
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:
81
+
82
+
```scala
83
+
xs.map: x =>
84
+
valy= x -1
85
+
y * y
86
+
```
87
+
and the following would also be legal:
88
+
```scala
89
+
xs.foldLeft(0): (x, y) =>
90
+
x + y
91
+
```
92
+
93
+
The grammar changes for this variant are as follows.
94
+
95
+
```
96
+
SimpleExpr ::= ...
97
+
| SimpleExpr ColonArgument
98
+
InfixExpr ::= ...
99
+
| InfixExpr id ColonArgument
100
+
ColonArgument ::= colon [LambdaStart]
101
+
indent (CaseClauses | Block) outdent
102
+
LambdaStart ::= FunParams (‘=>’ | ‘?=>’)
103
+
| HkTypeParamClause ‘=>’
104
+
```
36
105
### Compatibility
37
106
38
107
The proposed solution changes the meaning of the following code fragments:
0 commit comments