Skip to content

Commit c598de9

Browse files
committed
Fix formatting for swift code blocks.
1 parent c5a43c3 commit c598de9

File tree

1 file changed

+50
-48
lines changed

1 file changed

+50
-48
lines changed

proposals/NNNN-autoreleasepool-signature.md

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,44 @@ accidentally unwrapping a `nil` value.
3030

3131
For example:
3232

33-
```swift
34-
func doWork() throws -> Result {
35-
var result: Result? = nil
36-
var error: ErrorProtocol? = nil
37-
autoreleasepool {
38-
do {
39-
... actual computation which hopefully assigns to result but might not ...
40-
} catch let e {
41-
error = e
42-
}
33+
```swift
34+
func doWork() throws -> Result {
35+
var result: Result? = nil
36+
var error: ErrorProtocol? = nil
37+
autoreleasepool {
38+
do {
39+
... actual computation which hopefully assigns to result but might not ...
40+
} catch let e {
41+
error = e
4342
}
44-
guard let result = result else {
45-
throw error!
46-
}
47-
return result!
4843
}
49-
```
44+
guard let result = result else {
45+
throw error!
46+
}
47+
return result!
48+
}
49+
```
5050

5151
## Proposed solution
5252

5353
I'd like to propose altering the signature of the standard library
5454
`autoreleasepool` function to allow for a generic return type, as well as
5555
allowing a `throw` of an error:
5656

57-
public func autoreleasepool<Result>(@noescape body: () throws -> Result) rethrows -> Result
57+
```swift
58+
public func autoreleasepool<Result>(@noescape body: () throws -> Result) rethrows -> Result
59+
```
5860

5961
The case above becomes much more clear and less error-prone since the compiler
6062
can enforce that exactly one of the error and result are used:
6163

62-
```swift
63-
func doWork() throws -> Result {
64-
return try autoreleasepool {
65-
... actual computation which either returns or throws ...
66-
}
64+
```swift
65+
func doWork() throws -> Result {
66+
return try autoreleasepool {
67+
... actual computation which either returns or throws ...
6768
}
68-
```
69+
}
70+
```
6971

7072
As an aside, since this proposes changing the signature already, I would like
7173
to further propose changing the argument label from `code` to `body`. This seems
@@ -76,15 +78,15 @@ but isn't central to this proposal.
7678

7779
The updated standard library function would read:
7880

79-
```swift
80-
public func autoreleasepool<Result>(@noescape body: () throws -> Result) rethrows -> Result {
81-
let pool = __pushAutoreleasePool()
82-
defer {
83-
__popAutoreleasePool(pool)
84-
}
85-
return try body()
81+
```swift
82+
public func autoreleasepool<Result>(@noescape body: () throws -> Result) rethrows -> Result {
83+
let pool = __pushAutoreleasePool()
84+
defer {
85+
__popAutoreleasePool(pool)
8686
}
87-
```
87+
return try body()
88+
}
89+
```
8890

8991
## Impact on existing code
9092

@@ -99,26 +101,26 @@ return type would be better.
99101
I also explored whether third-party code could wrap `autoreleasepool` themselves
100102
with something like:
101103

102-
```swift
103-
func autoreleasepool_generic<ResultType>(@noescape code: Void throws -> ResultType) rethrows -> ResultType {
104-
var result:ResultType?
105-
var error:ErrorProtocol?
106-
107-
autoreleasepool {
108-
do {
109-
result = try code()
110-
} catch let e {
111-
error = e
112-
}
113-
}
114-
115-
if let result = result {
116-
return result
104+
```swift
105+
func autoreleasepool_generic<ResultType>(@noescape code: Void throws -> ResultType) rethrows -> ResultType {
106+
var result:ResultType?
107+
var error:ErrorProtocol?
108+
109+
autoreleasepool {
110+
do {
111+
result = try code()
112+
} catch let e {
113+
error = e
117114
}
118-
119-
throw error! // Doesn't compile.
120115
}
121-
```
116+
117+
if let result = result {
118+
return result
119+
}
120+
121+
throw error! // Doesn't compile.
122+
}
123+
```
122124

123125
but this doesn't compile, since in a function with `rethrows`, only the call to
124126
the passed in function that is marked as `throws` is allowed to throw.

0 commit comments

Comments
 (0)