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
vote-text: The following proposal needs to be updated, since only the specialized case version (with new lines) has been accepted. For more information, check the <a href="http://docs.scala-lang.org/sips/minutes/sip-20th-september-minutes.html">minutes</a>.
When using a comma-separated sequence of elements on multiple lines, such as:
33
35
@@ -39,33 +41,33 @@ Seq(
39
41
)
40
42
{% endhighlight %}
41
43
42
-
It is quite inconvenient to remove or comment out any element because one has to think about the fact that the last element mustn't have a trailing comma:
44
+
It is inconvenient to remove or comment out elements because the last element mustn't have a trailing comma:
43
45
44
46
{% highlight scala %}
45
-
Map(
47
+
Seq(
46
48
foo,
47
-
bar //,
49
+
bar,
48
50
// baz
49
-
)
51
+
) // error: illegal start of simple expression
50
52
{% endhighlight %}
51
53
52
-
Secondly, it is quite inconvenient to re-order the sequence, for instance if you wanted `baz` before `bar` you need to micromanage which is followed by a comma and which isn't:
54
+
It is also inconvenient to reorder because every element but the last one must be followed by a comma:
53
55
54
56
{% highlight scala %}
55
57
val xs = Seq(
56
58
foo,
57
-
baz // This isn't going to work
59
+
baz
58
60
bar,
59
-
)
61
+
) // error: illegal start of simple expression
60
62
{% endhighlight %}
61
63
62
-
### Reduce diff noise
64
+
### Diff noise reduction
63
65
64
-
Allowing trailing commas also reduces a lot of noise in diffs, such as:
66
+
Adding and removing commas also introduces unnecessary noise in diffs:
65
67
66
68
{% highlight diff %}
67
69
@@ -4,7 +4,8 @@
68
-
Map(
70
+
Seq(
69
71
foo,
70
72
bar,
71
73
- baz
@@ -76,15 +78,15 @@ Allowing trailing commas also reduces a lot of noise in diffs, such as:
76
78
77
79
### VCS authorship attribution
78
80
79
-
Using the example above, the authorship of the`baz`line would be preserved, instead of becoming that of the author of the`quux` line.
81
+
Using the example above, adding a comma after`baz`also unnecessarily changed the authorship of the line.
80
82
81
83
### Simplify code generation
82
84
83
-
Such a feature would also simplify generating Scala source code.
85
+
Allowing trailing commas would also simplify generating Scala source code.
84
86
85
87
### Long standing ticket
86
88
87
-
There is an open ticket ([SI-4986][]) where this feature was requested, referencing the fact that it facilitates code generation by tools and allows for easier sorting of the values, initially in the context of import selectors but later also for other constructs in the syntax.
89
+
([SI-4986][]) was opened in 2011 requesting support for trailing commas, referencing that it facilitates code generation by tools and allows easier sorting of values. It was initially in the context of import selectors but later also for other constructs in the syntax.
88
90
89
91
### Real-world use-cases
90
92
@@ -95,45 +97,120 @@ Some real-world use-cases where elements of a sequence are typically added, remo
95
97
96
98
## Design Decisions
97
99
98
-
There are a number of different elements of the Scala syntax that are comma separated, but instead of changing them all a subset of the more useful ones was chosen:
100
+
### Multi-line
101
+
102
+
It is not the intent of introducing trailing commas to promote a code style such as:
103
+
104
+
{% highlight scala %}
105
+
val xs = Seq(foo, baz, bar, )
106
+
{% endhighlight %}
107
+
108
+
for a number of reasons:
109
+
110
+
1. Subjectively, it's an ugly style.
111
+
2. Some people utilise commas as a mechanism for counting, so introducing an optional trailing commas interferes with this technique; when elements are one by line, then line-counting can be used.
112
+
3. Adding or removing elements is less cumbersome on one line.
113
+
4. Commenting out elements isn't any less cumbersome with an optional trailing comma.
114
+
115
+
Trailing comma support is therefore restricted to only comma-separated elements that are on separate lines:
116
+
117
+
{% highlight scala %}
118
+
val xs = Seq(
119
+
foo,
120
+
baz,
121
+
bar,
122
+
)
123
+
{% endhighlight %}
124
+
125
+
### What parts of the Scala grammar to change
126
+
127
+
There are a number of different parts of the Scala grammar that are comma-separated and, therefore, could support trailing commas. Specifically:
128
+
129
+
*`ArgumentExprs`
130
+
*`Params` and `ClassParams`
131
+
*`SimpleExpr1`
132
+
*`TypeArgs`, `TypeParamClause` and `FunTypeParamClause`
133
+
*`SimpleType` and `FunctionArgTypes`
134
+
*`SimplePattern`
135
+
*`ImportSelectors`
136
+
*`Import`
137
+
*`Bindings`
138
+
*`ids`, `ValDcl`, `VarDcl`, `VarDef` and `PatDef`
99
139
100
-
* tuples
101
-
* argument and parameter groups, including for implicits, for functions, methods and constructors
102
-
* import selectors
140
+
With this proposal I would like to present 2 variants:
103
141
104
-
From the spec these are:
142
+
1. The first variant adds trailing comma support to only multi-line `ArgumentExprs`, `Params` and `ClassParams`, which I consider to be the parts of the grammar that would most benefit from trailing commas.
105
143
106
-
* SimpleExpr1, ArgumentExprs via Exprs
107
-
* ParamClause, ParamClauses via Params
108
-
* ClassParamClause, ClassParamClauses via ClassParams
109
-
* ImportSelector
144
+
2. The second variant adds trailing comma support to the whole grammar (again, only for multi-line), which means more consistency, but also supporting trailing commas in places that in practice don't really require it as much, such as `ids`.
110
145
111
-
The elements that have not changed are:
146
+
In this proposal, only the first variant is considered: trailing comma support for `ArgumentExprs`, `Params` and `ParamClasses` for the sake of simplicity.
112
147
113
-
* ValDcl, VarDcl, VarDef via ids
114
-
* Type via FunctionArgTypes
115
-
* SimpleType, TypeArgs via Types
116
-
* Expr, ResultExpr via Bindings
117
-
* SimplePattern via Patterns
118
-
* TypeParamClause, FunTypeParamClause
119
-
* ImportExp
120
-
* PatDef
148
+
See below for more details on what that would mean.
The implementation is a simple change to the parser, allowing for a trailing comma, for the groups detailed above, and has been proposed in[scala/scala#5245][].
203
+
The implementation of trailing commas is is limited to changing Parsers.scala in the Scala compiler. An implementation of this proposal can be found at[scala/scala#5245][].
125
204
126
205
## Drawbacks/Trade-offs
127
206
128
-
The drawback, or trade-off, to this change is that it adds another way in which it is possible to do something in Scala. But it is the opinion of this SIP that the pragmatic advantage of being able to have trailing commas is worth this drawback.
207
+
One drawback, or trade-off, to this change is that it adds an alternative way in which it is possible to do something in Scala. But I believe that the pragmatic advantage of being able to have trailing commas is worth this drawback.
129
208
130
-
Given that this is a change in syntax, another drawback is that it requires changing the existing tools, such as those that parse Scala: intellij-scala, scalariform, scala.meta and scalaparse.
209
+
Another drawback, given this is a change in syntax, is that it requires changing the existing tools, such as those that parse Scala: intellij-scala, scalariform, scala.meta and scalaparse.
131
210
132
211
## Alternatives
133
212
134
-
As an alternative, trailing commas support could be added universally to all the comma-separated elements of the syntax. This would mean changing more (but still only in the parser), but it would make it consistent.
135
-
136
-
As an alternative to changing the language, there already exists today a compiler plugin called [scala-commas][] that provides this feature. It also provides some evidence that people would even use unsupported compiler apis and reflection to add this functionality, even when such a plugin won't compose with other plugins well, though arguably only weak evidence as it's a young and obscure plugin.
213
+
As an alternative to changing the language, there already exists today a compiler plugin called [scala-commas][] that provides a variant of this feature. It also provides some evidence that people would even use unsupported compiler apis and reflection to add this functionality, even when such a plugin won't compose with other plugins well, though arguably only weak evidence as it's a young and obscure plugin.
137
214
138
215
## References
139
216
@@ -144,3 +221,5 @@ As an alternative to changing the language, there already exists today a compile
0 commit comments