Conditional conformance in Swift 4.1

According to the status information in SE-0143, conditional conformance has been implemented in Swift 4.2. However, the following example from that proposal already compiles in Swift 4.1 (Xcode 9.3):

struct SomeWrapper<Wrapped> {
    let wrapped: Wrapped

extension SomeWrapper: Equatable where Wrapped: Equatable {
    static func ==(lhs: SomeWrapper<Wrapped>, rhs: SomeWrapper<Wrapped>) -> Bool {
        return lhs.wrapped == rhs.wrapped

In the CHANGELOG the only information related to SE-0143 that I found is the automatic synthesis of Equatable/Hashable conformance for arrays etc, but nothing about the implementation of conditional conformance in general.

I am just curious:

  • Is that feature missing in the CHANGELOG?
  • How could I have found out (apart from try and error) that conditional conformance is implemented in Swift 4.1?

Perhaps I just overlooked or misread something? This is such an important (and long desired) feature that I wonder why it is not prominently mentioned for Swift 4.1.

1 Like

The changelog says:

In Swift 4.1:

SE-0143 The standard library types Optional, Array, ArraySlice, ContiguousArray, and Dictionary now conform to the Equatable protocol when their element types conform to Equatable. This allows the == operator to compose (e.g., one can compare two values of type [Int : [Int?]?] with ==), as well as use various algorithms defined for Equatable element types, such as index(of:).

In Swift 4.2:

SE-0143 Runtime query of conditional conformances is now implemented. Therefore, a dynamic cast such as value as? P, where the dynamic type of value conditionally conforms to P, will succeed when the conditional requirements are met.

I feel like you were reading the changelogs wrongly. The implemented stuff is always below the Swift version. That said, this feature was implemented in Swift 4.1 but the support for dynamic casts is missing in Swift 4.1 which landed into the 4.2 branch.

Im a talking about Swift 4.1, not Swift 4.2. Your quote for Swift 4.1 is about the automatic synthesis of Equatable for certain collection types if their elements are Equatable, not about conditional conformance in general.

In the original post you’re claiming that the feature is implemented only in Swift 4.2 but already compiles in 4.1 which makes less sense.

If you would have read the quote carefully you’d understand that it’s talking not about automatic synthesis but about conditional conformances re-work that allowed types like Array conform to Equatable when the Element type is also Equatable. It even mentions explicitly the proposal number.

The standard library types Optional, Array, ArraySlice, ContiguousArray, and Dictionary now conform to the Equatable protocol when their element types conform to Equatable. <-- THIS ARE CONDITIONAL CONFORMANCES

SE-0166 / SE-0143

The standard library now defines the conformances of Optional, Array, Dictionary, and Set to Encodable and Decodable as conditional conformances, available only when their type parameters conform to Encodable or Decodable, respectively.

And the synthesis stuff is a totally different proposal:


Structs and enums that declare a conformance to Equatable/Hashable now get an automatically synthesized implementation of ==/hashValue. […]

There is also a dedicated chapter about conditional conformances in the Swift 4.1 book Conditionally Conforming to a Protocol.

PS: About your edit in the original post. The status says implicitly it’s fully implemented in Swift 4.2 while it’s partly implemented in Swift 4.1, since the dynamic cast support was missing.

I’m glad I could help you out ;)

I said that according to the status information in SE-0143 conditional conformance is implemented 4.2, but apparently already works in 4.1.

I did. The quotes are about some standard library types and certain protocols. That are special cases of conditional conformance.

Now that’s what I was looking for! It says (

A generic type may be able to satisfy the requirements of a protocol only under certain conditions, such as when the type’s generic parameter conforms to the protocol. You can make a generic type conditionally conform to a protocol by listing constraints when extending the type.

I still think that this should be mentioned in the change log for Swift 4.1.



No, Swift 4.1 does not support dynamically querying conditional conformance. See