Skip to content

Commit d4a9d51

Browse files
committed
[Changelog] Add a note about conformance synthesis in same-file extensions.
1 parent bcb98fa commit d4a9d51

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,48 @@ Swift 5.0
5757
Swift 4.2
5858
---------
5959

60+
* [SE-0185][]
61+
62+
Protocol conformances are now synthesized in extensions in the same file as
63+
the type definition, allowing automatic synthesis of conditional conformances
64+
to `Hashable`, `Equatable` and `Codable` (both `Encodable` and
65+
`Decodable`). For instance, if there is a generic wrapper type that can only
66+
be `Equatable` when its wrapped type is also `Equatable`, the `==` method can
67+
be automatically constructed by the compiler:
68+
69+
```swift
70+
struct Generic<Param> {
71+
var property: Param
72+
}
73+
74+
extension Generic: Equatable where Param: Equatable {}
75+
// Automatically synthesized inside the extension:
76+
// static func ==(lhs: Generic, rhs: Generic) -> Bool {
77+
// return lhs.property == rhs.property
78+
// }
79+
```
80+
81+
Code that wants to be as precise as possible should generally not
82+
conditionally conform to `Codable` directly, but rather its two constituent
83+
protocols `Encodable` and `Decodable`, or else one can only (for instance)
84+
decode a `Generic<Param>` if `Param` is `Encodable` in addition to
85+
`Decodable`, even though `Encodable` is likely not required:
86+
87+
```swift
88+
// Unnecessarily restrictive:
89+
extension Generic: Codable where Param: Codable {}
90+
91+
// More precise:
92+
extension Generic: Encodable where Param: Encodable {}
93+
extension Generic: Decodable where Param: Decodable {}
94+
```
95+
96+
Finally, due to `Decodable` having an `init` requirement, it is not possible
97+
to conform to `Decodable` in an extension of a non-final class: such a class
98+
needs to have any `init`s from protocols be `required`, which means they need
99+
to be in the class definition.
100+
101+
60102
* [SE-0054][]
61103

62104
`ImplicitlyUnwrappedOptional<T>` is now an unavailable typealias of `Optional<T>`.

0 commit comments

Comments
 (0)