Skip to content

Commit 7f5f363

Browse files
[SE-0439] Format code examples to multiple lines (#2509)
1 parent a4e4e8c commit 7f5f363

File tree

1 file changed

+114
-31
lines changed

1 file changed

+114
-31
lines changed

proposals/0439-trailing-comma-lists.md

Lines changed: 114 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ Other comma-separated lists in the language could also benefit from the flexibil
3434
let numbers = [1, 2, 0, 3, 4, 0, 0, 5]
3535

3636
let subsequences = numbers.split(
37-
separator: 0,
38-
// maxSplits: 1
37+
separator: 0,
38+
// maxSplits: 1
3939
) Unexpected ',' separator
4040
```
4141

@@ -52,65 +52,122 @@ This proposal adds support for trailing commas in comma-separated lists when the
5252
- Tuples and tuple patterns.
5353

5454
```swift
55-
(1, 2,)
56-
let block: (Int, Int,) -> Void = { (a, b,) in }
57-
let (a, b,) = (1, 2,)
58-
for (a, b,) in zip(s1, s2) { }
55+
56+
let velocity = (
57+
1.66007664274403694e-03,
58+
7.69901118419740425e-03,
59+
6.90460016972063023e-05,
60+
)
61+
62+
let (
63+
velocityX,
64+
velocityY,
65+
velocityZ,
66+
) = velocity
67+
5968
```
6069

6170
- Parameter and argument lists of initializers, functions, enum associated values, expression macros, attributes, and availability specs.
6271

6372
```swift
6473

65-
func foo(a: Int, b: Int,) { }
74+
func foo(
75+
input1: Int = 0,
76+
input2: Int = 0,
77+
) { }
6678

67-
foo(a: 1, b: 1,)
79+
foo(
80+
input1: 1,
81+
input2: 1,
82+
)
6883

6984
struct S {
70-
init(a: Int, b: Int,) { }
85+
init(
86+
input1: Int = 0,
87+
input2: Int = 0,
88+
) { }
7189
}
7290

7391
enum E {
74-
case foo(a: Int, b: Int,)
92+
case foo(
93+
input1: Int = 0,
94+
input2: Int = 0,
95+
)
7596
}
7697

77-
@Foo(1, 2, 3,)
98+
@Foo(
99+
"input 1",
100+
"input 2",
101+
"input 3",
102+
)
78103
struct S { }
79104

80-
f(_: @foo(1, 2,) Int)
81-
82-
#foo(1, 2,)
105+
#foo(
106+
"input 1",
107+
"input 2",
108+
"input 3",
109+
)
83110

84111
struct S {
85-
#foo(1, 2,)
112+
#foo(
113+
"input 1",
114+
"input 2",
115+
"input 3",
116+
)
86117
}
87118

88-
if #unavailable(iOS 15, watchOS 9,) { }
119+
if #unavailable(
120+
iOS 15,
121+
watchOS 9,
122+
) { }
89123

90124
```
91125
- Subscripts, including key path subscripts.
92126

93127
```swift
94-
let value = m[x, y,]
128+
let value = m[
129+
x,
130+
y,
131+
]
95132

96-
let keyPath = \Foo.bar[x,y,]
133+
let keyPath = \Foo.bar[
134+
x,
135+
y,
136+
]
97137

98-
f(\.[x,y,])
138+
f(\.[
139+
x,
140+
y,
141+
])
99142
```
100143

101144
- `if`, `guard` and `while` condition lists.
102145

103146
```swift
104-
if a, b, { }
105-
while a, b, { }
106-
guard a, b, else { }
147+
if
148+
condition1,
149+
condition2,
150+
{ }
151+
152+
while
153+
condition1,
154+
condition2,
155+
{ }
156+
157+
guard
158+
condition1,
159+
condition2,
160+
else { }
107161
```
108162

109163
- `switch` case labels.
110164

111165
```swift
112166
switch number {
113-
case 1, 2,:
167+
case
168+
1,
169+
2,
170+
:
114171
...
115172
default:
116173
..
@@ -120,25 +177,44 @@ switch number {
120177
- Closure capture lists.
121178

122179
```swift
123-
{ [a, b,] in }
180+
{ [
181+
capturedValue1,
182+
capturedValue2,
183+
] in
184+
}
124185
```
125186

126187
- Inheritance clauses.
127188

128189
```swift
129-
struct S: P1, P2, P3, { }
190+
struct S:
191+
P1,
192+
P2,
193+
P3,
194+
{ }
130195
```
131196

132197
- Generic parameters.
133198

134199
```swift
135-
struct S<T1, T2, T3,> { }
200+
struct S<
201+
T1,
202+
T2,
203+
T3,
204+
> { }
136205
```
137206

138207
- Generic `where` clauses.
139208

140209
```swift
141-
struct S<T1, T2, T3> where T1: P1, T2: P2, { }
210+
struct S<
211+
T1,
212+
T2,
213+
T3
214+
> where
215+
T1: P1,
216+
T2: P2,
217+
{ }
142218
```
143219

144220
- String interpolation
@@ -157,27 +233,34 @@ Enum case label lists:
157233

158234
```swift
159235
enum E {
160-
case a, b, c, // ❌ Expected identifier after comma in enum 'case' declaration
236+
case
237+
a,
238+
b,
239+
c, // ❌ Expected identifier after comma in enum 'case' declaration
161240
}
162241
```
163242

164243
Inheritance clauses for associated types in a protocol declaration:
165244

166245
```swift
167246
protocol Foo {
168-
associatedtype T: P1, P2, // ❌ Expected type
247+
associatedtype T:
248+
P1,
249+
P2, // ❌ Expected type
169250
}
170251
```
171252

172253
Generic `where` clauses for initializers and functions in a protocol declaration:
173254

174255
```swift
175256
protocol Foo {
176-
func f<T1, T2>(a: T1, b: T2) where T1: P1, T2: P2, // ❌ Expected type
257+
func f<T1, T2>(a: T1, b: T2) where
258+
T1: P1,
259+
T2: P2, // ❌ Expected type
177260
}
178261
```
179262

180-
Trailing commas will be allowed in single-element lists but not in zero-element lists, since the trailing comma is actually attached to the last element. Supporting a zero-element list would require supporting _leading_ commas, which isn't what this proposal is about.
263+
Trailing commas will be allowed in single-element lists but not in zero-element lists, since the trailing comma is actually attached to the last element. Supporting a zero-element list would require supporting _leading_ commas, which isn't what this proposal is about.
181264

182265
```swift
183266
(1,) // OK

0 commit comments

Comments
 (0)