Skip to content

Commit 6fbf0e0

Browse files
committed
Inline proposal spec from doc page
1 parent 53e9679 commit 6fbf0e0

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

content/fewer-braces.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,77 @@ It seems very natural to generalize the current class syntax indentation syntax
3131

3232
## Proposed solution
3333

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:
3535

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+
import language.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+
val file = Path.userHome / ".credentials"
67+
if file.exists
68+
then Seq(Credentials(file))
69+
else Seq()
70+
```
71+
72+
or
73+
74+
```scala
75+
xs.map:
76+
x =>
77+
val y = 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+
val y = 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+
```
36105
### Compatibility
37106

38107
The proposed solution changes the meaning of the following code fragments:

0 commit comments

Comments
 (0)