You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
@@ -52,33 +52,54 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
52
52
pool, `Sendable` checking will be performed, so the compiler will emit a
53
53
diagnostic in the call to `f` if `c` is not of `Sendable` type.
54
54
55
-
*[SE-0353][]:
55
+
*[SE-0350][]:
56
56
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.
59
58
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)
62
67
```
63
68
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]:
70
70
71
71
```swift
72
-
structAnyCollection<T> {
73
-
var wrapped: anyCollection<T>
74
-
}
75
-
76
-
let arrayOfCollections: [AnyCollection<T>] = [ /**/ ]
72
+
let regex =#/a[bc]+/#
77
73
```
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
+
79
84
*[SE-0329][]:
80
85
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.
`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
+
82
103
*[SE-0309][]:
83
104
84
105
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
89
110
`any` type having the same constraints as the associated type. For example:
`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:
99
134
100
135
```swift
101
-
let clock =ContinuousClock()
102
-
let elapsed = clock.measure {
103
-
someLongRunningWork()
136
+
protocolGraph<Vertex, Edge> {
137
+
associatedtypeVertex
138
+
associatedtypeEdge
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
+
funcshortestPath<V, E>(_: some Graph<V, E>, from: V, to: V) -> [E]
147
+
148
+
extensionGraph<Int, String> {...}
149
+
150
+
funcbuild() ->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
+
funcshortestPath<V, E, G>(_: G, from: V, to: V) -> [E]
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: anyCollection<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
+
structAnyCollection<T> {
185
+
var wrapped: anyCollection<T>
104
186
}
187
+
188
+
let arrayOfCollections: [AnyCollection<T>] = [ /**/ ]
105
189
```
106
190
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
+
107
198
* 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:
108
199
109
200
```swift
@@ -203,7 +294,7 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
203
294
in places that would previously fail because `any` types do not conform
204
295
to their protocols. For example:
205
296
206
-
```
297
+
```swift
207
298
protocol P {
208
299
associatedtypeA
209
300
funcgetA() -> A
@@ -421,12 +512,12 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
421
512
structS {
422
513
@available(macOS99, *) // error: stored properties cannot be marked potentially unavailable with '@available'
423
514
lazyvar a: Int=42
424
-
515
+
425
516
@available(macOS99, *) // error: stored properties cannot be marked potentially unavailable with '@available'
426
517
@Wrappervar b: Int
427
518
}
428
519
```
429
-
520
+
430
521
* 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.
431
522
432
523
```swift
@@ -452,7 +543,7 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
452
543
453
544
*[SE-0328][]:
454
545
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
456
547
within a result type, including having multiple opaque types in the same
0 commit comments