@@ -24,6 +24,86 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
24
24
}
25
25
```
26
26
27
+ * [ SE-0309] [ ] :
28
+
29
+ Protocols with associated types and ` Self ` requirements can now be used as the
30
+ types of values with the ` any ` keyword.
31
+
32
+ Protocol methods that return associated types can be called on an ` any ` type;
33
+ the result is type-erased to the associated type's upper bound, which is another
34
+ ` any ` type having the same constraints as the associated type. For example:
35
+
36
+ ``` swift
37
+ protocol Surface {... }
38
+
39
+ protocol Solid {
40
+ associatedtype SurfaceType : Surface
41
+ func boundary () -> SurfaceType
42
+ }
43
+
44
+ let solid: any Solid = ...
45
+
46
+ // Type of 'boundary' is 'any Surface'
47
+ let boundary = solid.boundary ()
48
+ ```
49
+
50
+ Protocol methods that take an associated type or ` Self ` cannot be used with ` any ` ,
51
+ however in conjunction with [ SE-0352] [ ] , you can pass the ` any ` type to a function
52
+ taking a generic parameter constrained to the protocol. Within the generic context,
53
+ type relationships are explicit and all protocol methods can be used.
54
+
55
+ * [ SE-0346] [ ] :
56
+
57
+ Protocols can now declare a list of one or more primary associated types:
58
+
59
+ ``` swift
60
+ protocol Graph <Vertex, Edge> {
61
+ associatedtype Vertex
62
+ associatedtype Edge
63
+ }
64
+ ```
65
+
66
+ A protocol-constrained type like ` Graph<Int> ` can now be written anywhere that
67
+ expects the right-hand side of a protocol conformance requirement:
68
+
69
+ ``` swift
70
+ func shortestPath <V , E >(_ : some Graph<V>, from : V, to : V) -> [E]
71
+
72
+ extension Graph <Int> {... }
73
+
74
+ func build () -> some Graph<String > {}
75
+ ```
76
+
77
+ A protocol-constrained type is equivalent to a conformance requirement to the protocol
78
+ itself together with a same-type requirement constraining the primary associated type.
79
+ The first two examples above are equivalent to the following:
80
+
81
+ ``` swift
82
+ func shortestPath <V , E , G >(_ : G, from : V, to : V) -> [E]
83
+ where G: Graph, G.Vertex == V, G.Edge == V
84
+
85
+ extension Graph where Vertex == Int {... }
86
+ ```
87
+
88
+ The ` build() ` function returning ` some Graph<String> ` cannot be written using a ` where `
89
+ clause; this is an example of a constrained opaque result type, which could not be written
90
+ before.
91
+
92
+ * [ SE-0353] [ ] :
93
+
94
+ Further generalizing the above, protocol-constrained types can also be used with ` any ` :
95
+
96
+ ``` swift
97
+ func findBestGraph (_ : [any Graph<Int >]) -> any Graph<Int > {... }
98
+ ```
99
+
100
+ * [ SE-0358] [ ] :
101
+
102
+ Various protocols in the standard library now declare primary associated types, for
103
+ example ` Sequence ` and ` Collection ` declare a single primary associated type ` Element ` .
104
+ For example, this allows writing down the types ` some Collection<Int> ` and
105
+ ` any Collection<Int> ` .
106
+
27
107
* 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:
28
108
29
109
``` swift
@@ -112,7 +192,7 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
112
192
in places that would previously fail because `any` types do not conform
113
193
to their protocols. For example:
114
194
115
- ```
195
+ ```swift
116
196
protocol P {
117
197
associatedtype A
118
198
func getA () -> A
@@ -349,7 +429,7 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
349
429
350
430
* [ SE-0328] [ ] :
351
431
352
- Opaque types (expressed with ' some' ) can now be used in structural positions
432
+ Opaque types (expressed with ` some ` ) can now be used in structural positions
353
433
within a result type, including having multiple opaque types in the same
354
434
result. For example:
355
435
@@ -832,7 +912,7 @@ Swift 5.5
832
912
}
833
913
834
914
835
- func hadWithdrawlOn (_ day : Date, from acct : BankAccount) async -> Bool {
915
+ func hadWithdrawalOn (_ day : Date, from acct : BankAccount) async -> Bool {
836
916
return await ! acct[day].allSatisfy { $0 .amount >= Amount.zero }
837
917
// ^~~~~~~~~ this access is async
838
918
}
@@ -961,7 +1041,7 @@ Swift 5.5
961
1041
}
962
1042
```
963
1043
964
- * The ' lazy' keyword now works in local contexts, making the following valid:
1044
+ * The ` lazy ` keyword now works in local contexts, making the following valid:
965
1045
966
1046
``` swift
967
1047
func test (useIt : Bool ) {
@@ -2807,7 +2887,7 @@ Swift 3.1
2807
2887
result in a compilation error.
2808
2888
2809
2889
Examples of functions that " return twice" include `vfork` and `setjmp`.
2810
- These functions change the control flow of a program in ways that that Swift
2890
+ These functions change the control flow of a program in ways that Swift
2811
2891
has never supported. For example, definitive initialization of variables,
2812
2892
a core Swift language feature, could not be guaranteed when these functions
2813
2893
were used.
@@ -2920,7 +3000,7 @@ Swift 3.0
2920
3000
2921
3001
* [SE- 0101 ][]:
2922
3002
2923
- The functions `sizeof ()`, `strideof ()`, and `alignof ()` have been removed.
3003
+ The functions `sizeof ()`, `strideof ()`, and `alignof ()` have been removed.
2924
3004
Memory layout properties for a type `T` are now spelled
2925
3005
`MemoryLayout < T> .size `, `MemoryLayout < T> .stride `, and
2926
3006
`MemoryLayout < T> .alignment `, respectively.
@@ -2962,7 +3042,7 @@ Swift 3.0
2962
3042
2963
3043
* [SE- 0124 ][]:
2964
3044
2965
- Initializers on `Int ` and `UInt ` that accept an `ObjectIdentifier ` must now use an explicit `bitPattern` label.
3045
+ Initializers on `Int ` and `UInt ` that accept an `ObjectIdentifier ` must now use an explicit `bitPattern` label.
2966
3046
2967
3047
```swift
2968
3048
let x: ObjectIdentifier = ...
@@ -3218,7 +3298,7 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
3218
3298
didFailToRegisterForRemoteNotificationsWithError error: NSError)
3219
3299
```
3220
3300
3221
- Now it accepts an `Error ` argument:
3301
+ Now it accepts an `Error ` argument:
3222
3302
3223
3303
```swift
3224
3304
optional func application (_ application: UIApplication,
@@ -3441,7 +3521,7 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
3441
3521
foo/* comment */ ! // no longer works
3442
3522
```
3443
3523
3444
- Parse errors resulting from this change can be resolved by moving the comment outside the expression.
3524
+ Parse errors resulting from this change can be resolved by moving the comment outside the expression.
3445
3525
3446
3526
* [SE- 0031 ][]:
3447
3527
@@ -3502,7 +3582,7 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
3502
3582
Attributes change from using `= ` in parameters lists
3503
3583
to using `: `, aligning with function call syntax.
3504
3584
3505
- ```
3585
+ ```swift
3506
3586
// before
3507
3587
@available (* , unavailable , renamed="MyRenamedProtocol")
3508
3588
@@ -3543,13 +3623,13 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
3543
3623
3544
3624
Curried function syntax (with successive parenthesized groups of arguments) is removed, and now produces a compile- time error. Use chained functional return types instead.
3545
3625
3546
- ```
3547
- // Before
3548
- public func project (function f: FunctionType)(p0 : CGPoint, p1 : CGPoint)(x : CGFloat) -> CGPoint
3626
+ ```swift
3627
+ // Before
3628
+ public func project (function f: FunctionType)(p0 : CGPoint, p1 : CGPoint)(x : CGFloat) -> CGPoint
3549
3629
3550
- // After
3551
- public func project (function f: FunctionType) -> (p0 : CGPoint, p1 : CGPoint) -> (x : CGFloat) -> CGPoint
3552
- ```
3630
+ // After
3631
+ public func project (function f: FunctionType) -> (p0 : CGPoint, p1 : CGPoint) -> (x : CGFloat) -> CGPoint
3632
+ ```
3553
3633
3554
3634
* Generic signatures can now contain superclass requirements with generic parameter types, for example:
3555
3635
@@ -4896,11 +4976,15 @@ Swift 1.2
4896
4976
* The `@autoclosure ` attribute is now an attribute on a parameter, not an
4897
4977
attribute on the parameter's type.
4898
4978
4899
- Where before you might have used:
4979
+ Where before you might have used
4900
4980
4901
4981
```swift
4902
4982
func assert (predicate : @autoclosure () -> Bool ) {... }
4903
- you now write this as :
4983
+ ```
4984
+
4985
+ you now write this as
4986
+
4987
+ ```swift
4904
4988
func assert (@autoclosure predicate : () -> Bool ) {... }
4905
4989
```
4906
4990
@@ -4942,7 +5026,11 @@ Swift 1.2
4942
5026
// redeclares Objective-C method
4943
5027
// 'setProperty:'
4944
5028
}
5029
+ ```
5030
+
4945
5031
Similar checking applies to accidental overrides in the Objective- C runtime:
5032
+
5033
+ ```swift
4946
5034
class B : NSObject {
4947
5035
func method (arg : String ) { } // note: overridden declaration
4948
5036
// here has type '(String) -> ()'
@@ -4953,7 +5041,11 @@ Swift 1.2
4953
5041
// selector 'method:' has incompatible
4954
5042
// type '([String]) -> ()'
4955
5043
}
5044
+ ```
5045
+
4956
5046
as well as protocol conformances:
5047
+
5048
+ ```swift
4957
5049
class MyDelegate : NSObject, NSURLSessionDelegate {
4958
5050
func URLSession (session : NSURLSession, didBecomeInvalidWithError :
4959
5051
Bool ){ } // error: Objective-C method 'URLSession:didBecomeInvalidWithError:'
@@ -5041,13 +5133,17 @@ Swift 1.2
5041
5133
that used `unsafeBitCast` as a workaround for this issue can be written to
5042
5134
use the raw value initializer.
5043
5135
5044
- For example:
5136
+ For example,
5045
5137
5046
5138
```swift
5047
5139
let animationCurve =
5048
5140
unsafeBitCast (userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue ,
5049
5141
UIViewAnimationCurve.self )
5050
- can now be written instead as :
5142
+ ```
5143
+
5144
+ can now be written instead as
5145
+
5146
+ ```swift
5051
5147
let animationCurve = UIViewAnimationCurve (rawValue :
5052
5148
userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue )!
5053
5149
```
@@ -9117,7 +9213,7 @@ Swift 1.0
9117
9213
[SE- 0107 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0107-unsaferawpointer.md>
9118
9214
[SE- 0108 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0108-remove-assoctype-inference.md>
9119
9215
[SE- 0109 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0109-remove-boolean.md>
9120
- [SE- 0110 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0110-distingish -single-tuple-arg.md>
9216
+ [SE- 0110 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0110-distinguish -single-tuple-arg.md>
9121
9217
[SE- 0111 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0111-remove-arg-label-type-significance.md>
9122
9218
[SE- 0112 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0112-nserror-bridging.md>
9123
9219
[SE- 0113 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0113-rounding-functions-on-floatingpoint.md>
@@ -9264,6 +9360,7 @@ Swift 1.0
9264
9360
[SE- 0300 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0300-continuation.md>
9265
9361
[SE- 0302 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0302-concurrent-value-and-concurrent-closures.md>
9266
9362
[SE- 0306 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0306-actors.md>
9363
+ [SE- 0309 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0309-unlock-existential-types-for-all-protocols.md>
9267
9364
[SE- 0310 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0310-effectful-readonly-properties.md>
9268
9365
[SE- 0311 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0311-task-locals.md>
9269
9366
[SE- 0313 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0313-actor-isolation-control.md>
@@ -9287,9 +9384,12 @@ Swift 1.0
9287
9384
[SE- 0341 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0341-opaque-parameters.md>
9288
9385
[SE- 0343 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0343-top-level-concurrency.md>
9289
9386
[SE- 0345 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0345-if-let-shorthand.md>
9387
+ [SE- 0346 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0346-light-weight-same-type-syntax.md>
9290
9388
[SE- 0347 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0347-type-inference-from-default-exprs.md>
9291
9389
[SE- 0349 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0349-unaligned-loads-and-stores.md>
9292
9390
[SE- 0352 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0352-implicit-open-existentials.md>
9391
+ [SE- 0353 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0353-constrained-existential-types.md>
9392
+ [SE- 0358 ]: < https: // github.com/apple/swift-evolution/blob/main/proposals/0358-primary-associated-types-in-stdlib.md>
9293
9393
9294
9394
[SR- 75 ]: < https: // bugs.swift.org/browse/SR-75>
9295
9395
[SR- 106 ]: < https: // bugs.swift.org/browse/SR-106>
0 commit comments