Skip to content

Commit 53e9679

Browse files
committed
First Draft
1 parent 31e756c commit 53e9679

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

content/fewer-braces.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
layout: sip
3+
permalink: /sips/:title.html
4+
stage: ready for review
5+
status: implemented
6+
title: SIP-NN Fewer Braces
7+
---
8+
9+
**By: Martin Odersky**
10+
11+
## History
12+
13+
| Date | Version |
14+
|---------------|--------------------|
15+
| July 1st 2022 | Initial Draft |
16+
17+
## Summary
18+
19+
The current state of Scala 3 makes braces optional around blocks and template definitions (i.e. bodies of classes, objects, traits, enums, or givens). This SIP proposes to allow optional braces also for function arguments.
20+
The advantages of doing so is that the language feels more systematic, and programs become typographically cleaner.
21+
The changes have been implemented and and made available under the language import `language.experimental.fewerBraces`. The proposal here is to make them available without a language import instead.
22+
23+
24+
## Motivation
25+
26+
After extensive experience with the current indentation rules I conclude that they are overall a big success.
27+
However. they still feel incomplete and a bit unsystematic since we can replace `{...}` in the majority of situations, but there are also important classes of situations where braces remain mandatory. In particular, braces are currently needed around blocks as function arguments.
28+
29+
It seems very natural to generalize the current class syntax indentation syntax to function arguments. In both cases, an indentation block is started by a colon at the end of a line.
30+
31+
32+
## Proposed solution
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.
35+
36+
### Compatibility
37+
38+
The proposed solution changes the meaning of the following code fragments:
39+
```scala
40+
val x = y:
41+
Int
42+
43+
val y = (xs.map: (Int => Int) =>
44+
Int)
45+
```
46+
In the first case, we have a type ascription where the type comes after the `:`. In the second case, we have
47+
a type ascription in parentheses where the ascribing function type is split by a newline. Note that we have not found examples like this in the dotty codebase or in the community build. We verified this by compiling everything with success with `fewerBraces` enabled. So we conclude that incompatibilities like these would be very rare.
48+
If there would be code using these idioms, it can be rewritten quite simply to avoid the problem. For instance, the following fragments would be legal (among many other possible variations):
49+
```scala
50+
val x = y
51+
: Int
52+
53+
val y = (xs.map: (Int => Int)
54+
=> Int)
55+
```
56+
57+
### Other concerns
58+
59+
Since this affects parsing, the scalameta parser and any other parser used in an IDE will also need to be updated. The necessary changes to the Scala 3 parser were made here: https://github.com/lampepfl/dotty/pull/15273/commits. The commit that embodies the core change set is here: https://github.com/lampepfl/dotty/pull/15273/commits/421bdd660b0456c2ff1ae386f032c41bb1e0212a.
60+
61+
### Open questions
62+
63+
None for me.
64+
65+
## Alternatives
66+
67+
I considered two variants:
68+
69+
The first variant would allow lambda parameters without preceding colons. E.g.
70+
```scala
71+
xs.foldLeft(z)(a, b) =>
72+
a + b
73+
```
74+
We concluded that this was visually less good since it looks too much like a function call `xs.foldLeft(z)(a, b)`.
75+
76+
The second variant would always require `(...)` around function types in ascriptions (which is in fact what the official syntax requires). That would have completely eliminated the second ambiguity above since
77+
```scala
78+
val y = (xs.map: (Int => Int) =>
79+
Int)
80+
```
81+
would then not be legal anyway. But it turned out that there were several community projects that were using function types in ascriptions without enclosing parentheses, so this change was deemed to break too much code.
82+
83+
84+
## Related work
85+
86+
- Doc page for proposed change: https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html#variant-indentation-marker--for-arguments
87+
88+
- Merged PR implementing the proposal under experimental flag: https://github.com/lampepfl/dotty/pull/15273/commits/421bdd660b0456c2ff1ae386f032c41bb1e0212a
89+
90+
- Latest discussion on contributors (there were several before when we discussed indentation in general): https://contributors.scala-lang.org/t/make-fewerbraces-available-outside-snapshot-releases/5024/166
91+
92+
## FAQ
93+

0 commit comments

Comments
 (0)