Skip to content

Commit c5a43c3

Browse files
committed
Add swift syntax highlighting to code blocks.
1 parent e38c9ea commit c5a43c3

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

proposals/NNNN-autoreleasepool-signature.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ accidentally unwrapping a `nil` value.
3030

3131
For example:
3232

33+
```swift
3334
func doWork() throws -> Result {
3435
var result: Result? = nil
3536
var error: ErrorProtocol? = nil
@@ -42,9 +43,10 @@ For example:
4243
}
4344
guard let result = result else {
4445
throw error!
45-
}
46-
return result!
47-
}
46+
}
47+
return result!
48+
}
49+
```
4850

4951
## Proposed solution
5052

@@ -57,11 +59,13 @@ allowing a `throw` of an error:
5759
The case above becomes much more clear and less error-prone since the compiler
5860
can enforce that exactly one of the error and result are used:
5961

62+
```swift
6063
func doWork() throws -> Result {
6164
return try autoreleasepool {
6265
... actual computation which either returns or throws ...
63-
}
64-
}
66+
}
67+
}
68+
```
6569

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

7377
The updated standard library function would read:
7478

79+
```swift
7580
public func autoreleasepool<Result>(@noescape body: () throws -> Result) rethrows -> Result {
7681
let pool = __pushAutoreleasePool()
7782
defer {
7883
__popAutoreleasePool(pool)
7984
}
8085
return try body()
8186
}
87+
```
8288

8389
## Impact on existing code
8490

@@ -93,6 +99,7 @@ return type would be better.
9399
I also explored whether third-party code could wrap `autoreleasepool` themselves
94100
with something like:
95101

102+
```swift
96103
func autoreleasepool_generic<ResultType>(@noescape code: Void throws -> ResultType) rethrows -> ResultType {
97104
var result:ResultType?
98105
var error:ErrorProtocol?
@@ -111,6 +118,7 @@ with something like:
111118

112119
throw error! // Doesn't compile.
113120
}
121+
```
114122

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

0 commit comments

Comments
 (0)