Skip to content

[Changelog] Add a note about conformance synthesis in same-file extensions. #16666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,48 @@ Swift 5.0
Swift 4.2
---------

* [SE-0185][]

Protocol conformances are now synthesized in extensions in the same file as
the type definition, allowing automatic synthesis of conditional conformances
to `Hashable`, `Equatable` and `Codable` (both `Encodable` and
`Decodable`). For instance, if there is a generic wrapper type that can only
be `Equatable` when its wrapped type is also `Equatable`, the `==` method can
be automatically constructed by the compiler:

```swift
struct Generic<Param> {
var property: Param
}

extension Generic: Equatable where Param: Equatable {}
// Automatically synthesized inside the extension:
// static func ==(lhs: Generic, rhs: Generic) -> Bool {
// return lhs.property == rhs.property
// }
```

Code that wants to be as precise as possible should generally not
conditionally conform to `Codable` directly, but rather its two constituent
protocols `Encodable` and `Decodable`, or else one can only (for instance)
decode a `Generic<Param>` if `Param` is `Encodable` in addition to
`Decodable`, even though `Encodable` is likely not required:

```swift
// Unnecessarily restrictive:
extension Generic: Codable where Param: Codable {}

// More precise:
extension Generic: Encodable where Param: Encodable {}
extension Generic: Decodable where Param: Decodable {}
```

Finally, due to `Decodable` having an `init` requirement, it is not possible
to conform to `Decodable` in an extension of a non-final class: such a class
needs to have any `init`s from protocols be `required`, which means they need
to be in the class definition.


* [SE-0054][]

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