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
| Sep 04th 2016 | New drawback: Cross building hinderance |
27
27
| Sep 12th 2016 | Remove cross building hinderance from drawbacks |
28
+
| Nov 12th 2016 | Major rework: multi-line, 2 variants & spec |
28
29
29
30
## Motivation
30
31
31
-
### Easy to modify lists
32
+
### Ease of modification
32
33
33
34
When using a comma-separated sequence of elements on multiple lines, such as:
34
35
@@ -40,33 +41,33 @@ Seq(
40
41
)
41
42
{% endhighlight %}
42
43
43
-
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:
44
45
45
46
{% highlight scala %}
46
-
Map(
47
+
Seq(
47
48
foo,
48
-
bar //,
49
+
bar,
49
50
// baz
50
-
)
51
+
) // error: illegal start of simple expression
51
52
{% endhighlight %}
52
53
53
-
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:
54
55
55
56
{% highlight scala %}
56
57
val xs = Seq(
57
58
foo,
58
-
baz // This isn't going to work
59
+
baz
59
60
bar,
60
-
)
61
+
) // error: illegal start of simple expression
61
62
{% endhighlight %}
62
63
63
64
### Reduce diff noise
64
65
65
-
Allowing trailing commas also reduces a lot of noise in diffs, such as:
66
+
Adding and removing commas also introduces unnecessary noise in diffs:
66
67
67
68
{% highlight diff %}
68
69
@@ -4,7 +4,8 @@
69
-
Map(
70
+
Seq(
70
71
foo,
71
72
bar,
72
73
- baz
@@ -77,15 +78,15 @@ Allowing trailing commas also reduces a lot of noise in diffs, such as:
77
78
78
79
### VCS authorship attribution
79
80
80
-
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.
81
82
82
83
### Simplify code generation
83
84
84
-
Such a feature would also simplify generating Scala source code.
85
+
Allowing trailing commas would also simplify generating Scala source code.
85
86
86
87
### Long standing ticket
87
88
88
-
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.
89
90
90
91
### Real-world use-cases
91
92
@@ -96,45 +97,113 @@ Some real-world use-cases where elements of a sequence are typically added, remo
96
97
97
98
## Design Decisions
98
99
99
-
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
+
Trailing comma support is therefore restricted to only comma-separated elements that are on separate lines:
109
+
110
+
{% highlight scala %}
111
+
val xs = Seq(
112
+
foo,
113
+
baz,
114
+
bar,
115
+
)
116
+
{% endhighlight %}
117
+
118
+
### What parts of the Scala grammar to change
119
+
120
+
There are a number of different parts of the Scala grammar that are comma-separated and, therefore, could support trailing commas. Specifically:
121
+
122
+
*`ArgumentExprs`
123
+
*`Params` and `ClassParams`
124
+
*`SimpleExpr1`
125
+
*`TypeArgs`, `TypeParamClause` and `FunTypeParamClause`
126
+
*`SimpleType` and `FunctionArgTypes`
127
+
*`SimplePattern`
128
+
*`ImportSelectors`
129
+
*`Import`
130
+
*`Bindings`
131
+
*`ids`, `ValDcl`, `VarDcl`, `VarDef` and `PatDef`
132
+
133
+
With this proposal I would like to present 2 variants:
134
+
135
+
1. The first variant adds trailing comma support to only `ArgumentExprs`, `Params` and `ClassParams`, which I consider to be the parts of the grammar that would most benefit from trailing commas.
100
136
101
-
* tuples
102
-
* argument and parameter groups, including for implicits, for functions, methods and constructors
103
-
* import selectors
137
+
2. The second variant adds trailing comma support to the whole grammar, which means more consistency, but also supporting trailing commas in places that doesn't really need it, such as `ids`.
104
138
105
-
From the spec these are:
139
+
**NOTE:** I recommend the first variant: only add trailing comma support to `ArgumentExprs`, `Params` and `ClassParams`.
106
140
107
-
* SimpleExpr1, ArgumentExprs via Exprs
108
-
* ParamClause, ParamClauses via Params
109
-
* ClassParamClause, ClassParamClauses via ClassParams
110
-
* ImportSelector
141
+
See below for a summary of what changing `ArgumentExprs`, `Params` and `ClassParams` means.
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][].
196
+
The implementation of trailing commas is a matter of changing some of the implementation of Scala's parser. An implementation of an earlier version of this proposal can be found at[scala/scala#5245][].
126
197
127
198
## Drawbacks/Trade-offs
128
199
129
-
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.
200
+
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.
130
201
131
-
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.
202
+
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.
132
203
133
204
## Alternatives
134
205
135
-
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.
136
-
137
-
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.
206
+
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.
0 commit comments