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
A redundant requirement does not indicate a coding error, and sometimes it is desirable to spell them
17
+
out for documentation purposes. For this reason these warnings are now disabled by default.
18
+
19
+
To restore the previous behavior, pass the `-Xfrontend -warn-redundant-requirements`
20
+
compiler flag.
21
+
8
22
*[SE-0338][]:
9
23
10
24
Non-isolated async functions now always execute on the global concurrent pool,
@@ -38,33 +52,54 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
38
52
pool, `Sendable` checking will be performed, so the compiler will emit a
39
53
diagnostic in the call to `f` if `c` is not of `Sendable` type.
40
54
41
-
*[SE-0353][]:
55
+
*[SE-0350][]:
42
56
43
-
Protocols with primary associated types can now be used in existential types,
44
-
enabling same-type constraints on those associated types.
57
+
The standard library has a new `Regex<Output>` type.
45
58
46
-
```
47
-
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)
48
67
```
49
68
50
-
Note that language features requiring runtime support like dynamic casts
51
-
(`is`, `as?`, `as!`), as well as generic usages of parameterized existentials
52
-
in generic types (e.g. `Array<any Collection<Int>>`) involve additional
53
-
availability checks to use. Back-deploying usages in generic position can be
54
-
worked around with a generic type-erasing wrapper struct, which is now much
55
-
simpler to implement:
69
+
Or via a [regex literal][SE-0354]:
56
70
57
71
```swift
58
-
structAnyCollection<T> {
59
-
var wrapped: anyCollection<T>
60
-
}
61
-
62
-
let arrayOfCollections: [AnyCollection<T>] = [ /**/ ]
72
+
let regex =#/a[bc]+/#
63
73
```
64
-
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
+
65
84
*[SE-0329][]:
66
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
+
68
103
*[SE-0309][]:
69
104
70
105
Protocols with associated types and `Self` requirements can now be used as the
@@ -75,21 +110,91 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
75
110
`any` type having the same constraints as the associated type. For example:
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:
134
+
135
+
```swift
136
+
protocolGraph<Vertex, Edge> {
137
+
associatedtypeVertex
138
+
associatedtypeEdge
81
139
}
82
140
```
83
141
84
-
`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.
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:
85
144
86
145
```swift
87
-
let clock =ContinuousClock()
88
-
let elapsed = clock.measure {
89
-
someLongRunningWork()
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>
90
186
}
187
+
188
+
let arrayOfCollections: [AnyCollection<T>] = [ /**/ ]
91
189
```
92
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
+
93
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:
94
199
95
200
```swift
@@ -189,7 +294,7 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
189
294
in places that would previously fail because `any` types do not conform
190
295
to their protocols. For example:
191
296
192
-
```
297
+
```swift
193
298
protocol P {
194
299
associatedtypeA
195
300
funcgetA() -> A
@@ -407,12 +512,12 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
407
512
structS {
408
513
@available(macOS99, *) // error: stored properties cannot be marked potentially unavailable with '@available'
409
514
lazyvar a: Int=42
410
-
515
+
411
516
@available(macOS99, *) // error: stored properties cannot be marked potentially unavailable with '@available'
412
517
@Wrappervar b: Int
413
518
}
414
519
```
415
-
520
+
416
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.
417
522
418
523
```swift
@@ -438,7 +543,7 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
438
543
439
544
*[SE-0328][]:
440
545
441
-
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
442
547
within a result type, including having multiple opaque types in the same
0 commit comments