Skip to content

Commit bbf7650

Browse files
CHANGELOG: Sync 5.7 section with main
Some entries were missing altogether or a link to the proposal, others were fragmented by accident when cherry-picked, and some code blocks were missing language modes.
1 parent cffe8a0 commit bbf7650

File tree

1 file changed

+127
-28
lines changed

1 file changed

+127
-28
lines changed

CHANGELOG.md

Lines changed: 127 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,54 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
5252
pool, `Sendable` checking will be performed, so the compiler will emit a
5353
diagnostic in the call to `f` if `c` is not of `Sendable` type.
5454

55-
* [SE-0353][]:
55+
* [SE-0350][]:
5656

57-
Protocols with primary associated types can now be used in existential types,
58-
enabling same-type constraints on those associated types.
57+
The standard library has a new `Regex<Output>` type.
5958

60-
```
61-
let strings: any Collection<String> = [ "Hello" ]
59+
This type represents an _extended regular expression_, allowing more fluent
60+
string processing operations. A `Regex` may be created by
61+
[initialization from a string][SE-0355]:
62+
63+
```swift
64+
let pattern = "a[bc]+" // matches "a" followed by one or more instances
65+
// of either "b" or "c"
66+
let regex = try! Regex(pattern)
6267
```
6368

64-
Note that language features requiring runtime support like dynamic casts
65-
(`is`, `as?`, `as!`), as well as generic usages of parameterized existentials
66-
in generic types (e.g. `Array<any Collection<Int>>`) involve additional
67-
availability checks to use. Back-deploying usages in generic position can be
68-
worked around with a generic type-erasing wrapper struct, which is now much
69-
simpler to implement:
69+
Or via a [regex literal][SE-0354]:
7070

7171
```swift
72-
struct AnyCollection<T> {
73-
var wrapped: any Collection<T>
74-
}
75-
76-
let arrayOfCollections: [AnyCollection<T>] = [ /**/ ]
72+
let regex = #/a[bc]+/#
7773
```
78-
74+
75+
In Swift 6, `/` will also be supported as a delimiter for `Regex` literals.
76+
You can enable this mode in Swift 5.7 with the `-enable-bare-slash-regex`
77+
flag. Doing so will cause some existing expressions that use `/` as an
78+
operator to no longer compile; you can add parentheses or line breaks as a
79+
workaround.
80+
81+
There are [new string-processing algorithms][SE-0357] that support
82+
`String`, `Regex` and arbitrary `Collection` types.
83+
7984
* [SE-0329][]:
8085
New types representing time and clocks were introduced. This includes a protocol `Clock` defining clocks which allow for defining a concept of now and a way to wake up after a given instant. Additionally a new protocol `InstantProtocol` for defining instants in time was added. Furthermore a new protocol `DurationProtocol` was added to define an elapsed duration between two given `InstantProtocol` types. Most commonly the `Clock` types for general use are the `SuspendingClock` and `ContinuousClock` which represent the most fundamental clocks for the system. The `SuspendingClock` type does not progress while the machine is suspended whereas the `ContinuousClock` progresses no matter the state of the machine.
8186

87+
```swift
88+
func delayedHello() async throws {
89+
try await Task.sleep(until: .now + .milliseconds(123), clock: .continuous)
90+
print("hello delayed world")
91+
}
92+
```
93+
94+
`Clock` also has methods to measure the elapsed duration of the execution of work. In the case of the `SuspendingClock` and `ContinuousClock` this measures with high resolution and is suitable for benchmarks.
95+
96+
```swift
97+
let clock = ContinuousClock()
98+
let elapsed = clock.measure {
99+
someLongRunningWork()
100+
}
101+
```
102+
82103
* [SE-0309][]:
83104

84105
Protocols with associated types and `Self` requirements can now be used as the
@@ -89,21 +110,91 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
89110
`any` type having the same constraints as the associated type. For example:
90111

91112
```swift
92-
func delayedHello() async throws {
93-
try await Task.sleep(until: .now + .milliseconds(123), clock: .continuous)
94-
print("hello delayed world")
113+
protocol Surface {...}
114+
115+
protocol Solid {
116+
associatedtype SurfaceType: Surface
117+
func boundary() -> SurfaceType
95118
}
119+
120+
let solid: any Solid = ...
121+
122+
// Type of 'boundary' is 'any Surface'
123+
let boundary = solid.boundary()
96124
```
97125

98-
`Clock` also has methods to measure the elapsed duration of the execution of work. In the case of the `SuspendingClock` and `ContinuousClock` this measures with high resolution and is suitable for benchmarks.
126+
Protocol methods that take an associated type or `Self` cannot be used with `any`,
127+
however in conjunction with [SE-0352][], you can pass the `any` type to a function
128+
taking a generic parameter constrained to the protocol. Within the generic context,
129+
type relationships are explicit and all protocol methods can be used.
130+
131+
* [SE-0346][]:
132+
133+
Protocols can now declare a list of one or more _primary associated types_, which enable writing same-type requirements on those associated types using angle bracket syntax:
99134

100135
```swift
101-
let clock = ContinuousClock()
102-
let elapsed = clock.measure {
103-
someLongRunningWork()
136+
protocol Graph<Vertex, Edge> {
137+
associatedtype Vertex
138+
associatedtype Edge
139+
}
140+
```
141+
142+
You can now write a protocol name followed by type arguments in angle brackets, like
143+
`Graph<Int, String>`, anywhere that a protocol conformance requirement may appear:
144+
145+
```swift
146+
func shortestPath<V, E>(_: some Graph<V, E>, from: V, to: V) -> [E]
147+
148+
extension Graph<Int, String> {...}
149+
150+
func build() -> some Graph<Int, String> {}
151+
```
152+
153+
A protocol name followed by angle brackets is shorthand for a conformance requirement,
154+
together with a same-type requirement for the protocol's primary associated types.
155+
The first two examples above are equivalent to the following:
156+
157+
```swift
158+
func shortestPath<V, E, G>(_: G, from: V, to: V) -> [E]
159+
where G: Graph, G.Vertex == V, G.Edge == E
160+
161+
extension Graph where Vertex == Int, Edge == String {...}
162+
```
163+
164+
The `build()` function returning `some Graph<Int, String>` can't be written using a
165+
`where` clause; this is an example of a constrained opaque result type, which is new expressivity in Swift 5.7.
166+
167+
* [SE-0353][]:
168+
169+
Protocols with primary associated types can now be used in existential types,
170+
enabling same-type constraints on those associated types.
171+
172+
```swift
173+
let strings: any Collection<String> = [ "Hello" ]
174+
```
175+
176+
Note that language features requiring runtime support like dynamic casts
177+
(`is`, `as?`, `as!`), as well as generic usages of parameterized existentials
178+
in generic types (e.g. `Array<any Collection<Int>>`) involve additional
179+
availability checks to use. Back-deploying usages in generic position can be
180+
worked around with a generic type-erasing wrapper struct, which is now much
181+
simpler to implement:
182+
183+
```swift
184+
struct AnyCollection<T> {
185+
var wrapped: any Collection<T>
104186
}
187+
188+
let arrayOfCollections: [AnyCollection<T>] = [ /**/ ]
105189
```
106190

191+
* [SE-0358][]:
192+
193+
Various protocols in the standard library now declare primary associated types, for
194+
example `Sequence` and `Collection` declare a single primary associated type `Element`.
195+
For example, this allows writing down the types `some Collection<Int>` and
196+
`any Collection<Int>`.
197+
107198
* References to `optional` methods on a protocol metatype, as well as references to dynamically looked up methods on `AnyObject` are now supported on par with other function references. The type of such a reference (formerly an immediate optional by mistake) has been altered to that of a function that takes a single argument and returns an optional value of function type:
108199

109200
```swift
@@ -203,7 +294,7 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
203294
in places that would previously fail because `any` types do not conform
204295
to their protocols. For example:
205296

206-
```
297+
```swift
207298
protocol P {
208299
associatedtype A
209300
func getA() -> A
@@ -421,12 +512,12 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
421512
struct S {
422513
@available(macOS 99, *) // error: stored properties cannot be marked potentially unavailable with '@available'
423514
lazy var a: Int = 42
424-
515+
425516
@available(macOS 99, *) // error: stored properties cannot be marked potentially unavailable with '@available'
426517
@Wrapper var b: Int
427518
}
428519
```
429-
520+
430521
* The compiler now correctly emits warnings for more kinds of expressions where a protocol conformance is used and may be unavailable at runtime. Previously, member reference expressions and type erasing expressions that used potentially unavailable conformances were not diagnosed, leading to potential crashes at runtime.
431522

432523
```swift
@@ -452,7 +543,7 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
452543

453544
* [SE-0328][]:
454545

455-
Opaque types (expressed with 'some') can now be used in structural positions
546+
Opaque types (expressed with `some`) can now be used in structural positions
456547
within a result type, including having multiple opaque types in the same
457548
result. For example:
458549

@@ -9367,6 +9458,7 @@ Swift 1.0
93679458
[SE-0300]: <https://github.com/apple/swift-evolution/blob/main/proposals/0300-continuation.md>
93689459
[SE-0302]: <https://github.com/apple/swift-evolution/blob/main/proposals/0302-concurrent-value-and-concurrent-closures.md>
93699460
[SE-0306]: <https://github.com/apple/swift-evolution/blob/main/proposals/0306-actors.md>
9461+
[SE-0309]: <https://github.com/apple/swift-evolution/blob/main/proposals/0309-unlock-existential-types-for-all-protocols.md>
93709462
[SE-0310]: <https://github.com/apple/swift-evolution/blob/main/proposals/0310-effectful-readonly-properties.md>
93719463
[SE-0311]: <https://github.com/apple/swift-evolution/blob/main/proposals/0311-task-locals.md>
93729464
[SE-0313]: <https://github.com/apple/swift-evolution/blob/main/proposals/0313-actor-isolation-control.md>
@@ -9391,9 +9483,16 @@ Swift 1.0
93919483
[SE-0341]: <https://github.com/apple/swift-evolution/blob/main/proposals/0341-opaque-parameters.md>
93929484
[SE-0343]: <https://github.com/apple/swift-evolution/blob/main/proposals/0343-top-level-concurrency.md>
93939485
[SE-0345]: <https://github.com/apple/swift-evolution/blob/main/proposals/0345-if-let-shorthand.md>
9486+
[SE-0346]: <https://github.com/apple/swift-evolution/blob/main/proposals/0346-light-weight-same-type-syntax.md>
93949487
[SE-0347]: <https://github.com/apple/swift-evolution/blob/main/proposals/0347-type-inference-from-default-exprs.md>
93959488
[SE-0349]: <https://github.com/apple/swift-evolution/blob/main/proposals/0349-unaligned-loads-and-stores.md>
9489+
[SE-0350]: <https://github.com/apple/swift-evolution/blob/main/proposals/0350-regex-type-overview.md>
93969490
[SE-0352]: <https://github.com/apple/swift-evolution/blob/main/proposals/0352-implicit-open-existentials.md>
9491+
[SE-0353]: <https://github.com/apple/swift-evolution/blob/main/proposals/0353-constrained-existential-types.md>
9492+
[SE-0354]: <https://github.com/apple/swift-evolution/blob/main/proposals/0354-regex-literals.md>
9493+
[SE-0355]: <https://github.com/apple/swift-evolution/blob/main/proposals/0355-regex-syntax-run-time-construction.md>
9494+
[SE-0357]: <https://github.com/apple/swift-evolution/blob/main/proposals/0357-regex-string-processing-algorithms.md>
9495+
[SE-0358]: <https://github.com/apple/swift-evolution/blob/main/proposals/0358-primary-associated-types-in-stdlib.md>
93979496

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

0 commit comments

Comments
 (0)