Skip to content

Commit 03f4b34

Browse files
authored
Merge pull request #31219 from ZevEisenberg/patch-1
Add syntax highlighting for GitHub
2 parents a48880d + bf5ff50 commit 03f4b34

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

userdocs/diagnostics/associated-type-requirements.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Using Protocols with `Self` or Associated Type Requirements
22
Protocols in Swift may be used as types, as part of a generic constraint, or as part of an opaque result type.
3-
```
3+
4+
```swift
45
// CustomStringConvertible can be used as a type.
56
func foo(bar: CustomStringConvertible) { /* ... */ }
67

@@ -12,7 +13,8 @@ func baz() -> some CustomStringConvertible { /* ... */ }
1213
```
1314

1415
While all Swift protocols can be used as generic constraints and as part of opaque result types, not all protocols can be used as types in general. Specifically, if a protocol has a requirement which references `Self` or an associated type, it cannot be used as a type. One such protocol is `Identifiable`, which has the requirement `var id: ID { get }`, where `ID` is an associated type. As a result, the following code is not allowed:
15-
```
16+
17+
```swift
1618
func foo(bar: Identifiable) { /* ... */ }
1719
// error: protocol 'Identifiable' can only be used as a generic constraint because it has Self or associated type requirements
1820
```

userdocs/diagnostics/complex-closure-inference.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# Inferring Closure Types
22
If a closure contains a single expression, Swift will consider its body in addition to its signature and the surrounding context when performing type inference. For example, in the following code the type of `doubler` is inferred to be `(Int) -> Int` using only its body:
3-
```
3+
4+
```swift
45
let doubler = {
56
$0 * 2
67
}
78
```
9+
810
If a closure body is not a single expression, it will not be considered when inferring the closure type. This is consistent with how type inference works in other parts of the language, where it proceeds one statement at a time. For example, in the following code an error will be reported because the type of `evenDoubler` cannot be inferred from its surrounding context and no signature was provided:
9-
```
11+
12+
```swift
1013
// error: unable to infer complex closure return type; add explicit type to disambiguate
1114
let evenDoubler = { x in
1215
if x.isMultiple(of: 2) {
@@ -16,14 +19,18 @@ let evenDoubler = { x in
1619
}
1720
}
1821
```
22+
1923
This can be fixed by providing additional contextual information:
20-
```
24+
25+
```swift
2126
let evenDoubler: (Int) -> Int = { x in
2227
// ...
2328
}
2429
```
30+
2531
Or by giving the closure an explicit signature:
26-
```
32+
33+
```swift
2734
let evenDoubler = { (x: Int) -> Int in
2835
// ...
2936
}

0 commit comments

Comments
 (0)