Skip to content

Commit bb03f77

Browse files
authored
Merge pull request #59028 from slavapestov/update-changelog-for-generics-features
Update CHANGELOG for new generics features in Swift 5.7
2 parents d559e09 + 79059ca commit bb03f77

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

CHANGELOG.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,86 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
55

66
## Swift 5.7
77

8+
* [SE-0309][]:
9+
10+
Protocols with associated types and `Self` requirements can now be used as the
11+
types of values with the `any` keyword.
12+
13+
Protocol methods that return associated types can be called on an `any` type;
14+
the result is type-erased to the associated type's upper bound, which is another
15+
`any` type having the same constraints as the associated type. For example:
16+
17+
```swift
18+
protocol Surface {...}
19+
20+
protocol Solid {
21+
associatedtype SurfaceType: Surface
22+
func boundary() -> SurfaceType
23+
}
24+
25+
let solid: any Solid = ...
26+
27+
// Type of 'boundary' is 'any Surface'
28+
let boundary = solid.boundary()
29+
```
30+
31+
Protocol methods that take an associated type or `Self` cannot be used with `any`,
32+
however in conjunction with [SE-0352][], you can pass the `any` type to a function
33+
taking a generic parameter constrained to the protocol. Within the generic context,
34+
type relationships are explicit and all protocol methods can be used.
35+
36+
* [SE-0346][]:
37+
38+
Protocols can now declare a list of one or more primary associated types:
39+
40+
```swift
41+
protocol Graph<Vertex, Edge> {
42+
associatedtype Vertex
43+
associatedtype Edge
44+
}
45+
```
46+
47+
A protocol-constrained type like `Graph<Int>` can now be written anywhere that
48+
expects the right-hand side of a protocol conformance requirement:
49+
50+
```swift
51+
func shortestPath<V, E>(_: some Graph<V>, from: V, to: V) -> [E]
52+
53+
extension Graph<Int> {...}
54+
55+
func build() -> some Graph<String> {}
56+
```
57+
58+
A protocol-constrained type is equivalent to a conformance requirement to the protocol
59+
itself together with a same-type requirement constraining the primary associated type.
60+
The first two examples above are equivalent to the following:
61+
62+
```swift
63+
func shortestPath<V, E, G>(_: G, from: V, to: V) -> [E]
64+
where G: Graph, G.Vertex == V, G.Edge == V
65+
66+
extension Graph where Vertex == Int {...}
67+
```
68+
69+
The `build()` function returning `some Graph<String>` cannot be written using a `where`
70+
clause; this is an example of a constrained opaque result type, which could not be written
71+
before.
72+
73+
* [SE-0353][]:
74+
75+
Further generalizing the above, protocol-constrained types can also be used with `any`:
76+
77+
```swift
78+
func findBestGraph(_: [any Graph<Int>]) -> any Graph<Int> {...}
79+
```
80+
81+
* [SE-0358][]:
82+
83+
Various protocols in the standard library now declare primary associated types, for
84+
example `Sequence` and `Collection` declare a single primary associated type `Element`.
85+
For example, this allows writing down the types `some Collection<Int>` and
86+
`any Collection<Int>`.
87+
888
* References to `optional` methods on a protocol metatype, as well as references to dynamically looked up methods on the `AnyObject` metatype are now supported. These references always have the type of a function that accepts a single argument and returns an optional value of function type:
989

1090
```swift
@@ -9261,6 +9341,7 @@ Swift 1.0
92619341
[SE-0300]: <https://github.com/apple/swift-evolution/blob/main/proposals/0300-continuation.md>
92629342
[SE-0302]: <https://github.com/apple/swift-evolution/blob/main/proposals/0302-concurrent-value-and-concurrent-closures.md>
92639343
[SE-0306]: <https://github.com/apple/swift-evolution/blob/main/proposals/0306-actors.md>
9344+
[SE-0309]: <https://github.com/apple/swift-evolution/blob/main/proposals/0309-unlock-existential-types-for-all-protocols.md>
92649345
[SE-0310]: <https://github.com/apple/swift-evolution/blob/main/proposals/0310-effectful-readonly-properties.md>
92659346
[SE-0311]: <https://github.com/apple/swift-evolution/blob/main/proposals/0311-task-locals.md>
92669347
[SE-0313]: <https://github.com/apple/swift-evolution/blob/main/proposals/0313-actor-isolation-control.md>
@@ -9283,9 +9364,12 @@ Swift 1.0
92839364
[SE-0341]: <https://github.com/apple/swift-evolution/blob/main/proposals/0341-opaque-parameters.md>
92849365
[SE-0343]: <https://github.com/apple/swift-evolution/blob/main/proposals/0343-top-level-concurrency.md>
92859366
[SE-0345]: <https://github.com/apple/swift-evolution/blob/main/proposals/0345-if-let-shorthand.md>
9367+
[SE-0346]: <https://github.com/apple/swift-evolution/blob/main/proposals/0346-light-weight-same-type-syntax.md>
92869368
[SE-0347]: <https://github.com/apple/swift-evolution/blob/main/proposals/0347-type-inference-from-default-exprs.md>
92879369
[SE-0349]: <https://github.com/apple/swift-evolution/blob/main/proposals/0349-unaligned-loads-and-stores.md>
92889370
[SE-0352]: <https://github.com/apple/swift-evolution/blob/main/proposals/0352-implicit-open-existentials.md>
9371+
[SE-0353]: <https://github.com/apple/swift-evolution/blob/main/proposals/0353-constrained-existential-types.md>
9372+
[SE-0358]: <https://github.com/apple/swift-evolution/blob/main/proposals/0358-primary-associated-types-in-stdlib.md>
92899373

92909374
[SR-75]: <https://bugs.swift.org/browse/SR-75>
92919375
[SR-106]: <https://bugs.swift.org/browse/SR-106>

0 commit comments

Comments
 (0)